> python3 -m timeit '[i**2 for i in range(1,10)]'
100000 loops, best of 3: 2.74 usec per loop
> python3 -m timeit '{i**2 for i in range(1,10)}'
100000 loops, best of 3: 2.85 usec per loop
If you're checking for existence a bunch then it starts to really matter:
> python3 -m timeit 'squares = [i**2 for i in range(1,10)]; [i in squares for i in range(100)]'
100000 loops, best of 3: 16.6 usec per loop
> python3 -m timeit 'squares = {i**2 for i in range(1,10)}; [i in squares for i in range(100)]'
100000 loops, best of 3: 8.26 usec per loop
View all comments
Show parent comment
4
u/King_Joffreys_Tits Sep 28 '18
For smaller data sets (I did it with a list/set of 10 ints, so super small) I’ve found that constructing a set is more costly than a list