Skip to content
Advertisement

Which is faster, a set comprehension or set difference? [closed]

I have two lists:

from secrets import token_urlsafe

a = [token_urlsafe() for i in range(100)]
b = a[50:]

I need to find the difference between the two lists. Do I use a set comprehension, or a set difference?

The Python documentation describes two ways to do this

Set Difference

set(a) - set(b)

Return a new set with elements in the set that are not in the others

ref

Set Comprehensions

{i for i in a if i not in b}

Use a set comprehension: {c for c in 'abracadabra' if c not in 'abc'}

ref

Advertisement

Answer

We can see that the set difference method is faster using timeit:

>>> timeit.timeit(lambda: set(a) - set(b), number=100000)
0.37637379999796394

>>> timeit.timeit(lambda: {i for i in a if i not in b}, number=100000)
5.430218600013177

Note that the time may vary for the length of set a, the length of set b, and maybe the number of elements in common, and the position for which they’re in common.

Advertisement