Skip to content Skip to sidebar Skip to footer

What Is The Preferred Way To Compose A Set From Multiple Lists In Python

I have a few different lists that I want to turn into a set and use to find the difference from another set. Let's call them A, B, and C. Is the more optimal way to do this set(A

Solution 1:

For small lists, set(A + B + C) works fine. For larger lists, the following is more efficient because it does not create a temporary list:

myset = set(A)
myset.update(B)
myset.update(C)

A different approach uses itertools.chain, which is also efficient because it does not create temporary list:

importitertoolsmyset= set(itertools.chain(A, B, C))

Solution 2:

Here is some timing experiments:

import numpy as np
import itertools

for r in [10,100,1000,10000]:
   A = list(np.random.randint(r, size=1000000))
   B = list(np.random.randint(r, size=1000000))

   %timeit set(A).update(B)
   %timeit set(A+B)
   %timeit set(itertools.chain(A, B))
   print('---')

Here is the results for size = 1000:

10000loops,best of 3:87.2µsperloop10000loops,best of 3:87.3µsperloop10000loops,best of 3:90.7µsperloop---10000loops,best of 3:88.2µsperloop10000loops,best of 3:86.8µsperloop10000loops,best of 3:89.4µsperloop---10000loops,best of 3:80.9µsperloop10000loops,best of 3:84.5µsperloop10000loops,best of 3:87µsperloop---10000loops,best of 3:97.4µsperloop10000loops,best of 3:102µsperloop10000loops,best of 3:107µsperloop

Here is the results for size = 1000000:

10loops,best of 3:89msperloop10loops,best of 3:106msperloop10loops,best of 3:98.4msperloop---10loops,best of 3:89.1msperloop10loops,best of 3:110msperloop10loops,best of 3:94.2msperloop---10loops,best of 3:94.9msperloop10loops,best of 3:109msperloop10loops,best of 3:105msperloop---10loops,best of 3:115msperloop10loops,best of 3:143msperloop10loops,best of 3:138msperloop

So, update() seems to be slightly faster than both other methods. However, I don't think that the time difference is significant.

Post a Comment for "What Is The Preferred Way To Compose A Set From Multiple Lists In Python"