This notebook explores how fast determining if some value is in lists, tuples, sets, and dictionaries.
See simplified conclusion at bottom of notebook.
All Your Ducks In A Row: Data Structures in the Standard Library and Beyond
In [1]:
    
n = 10**7
a = list(range(n))
b = tuple(a)
c = set(a)
d = dict(zip(a, a))
    
In [2]:
    
5 in a, 5 in b, 5 in c, 5 in d
    
    Out[2]:
In [3]:
    
i = n/2
    
In [4]:
    
%timeit i in a
    
    
In [5]:
    
%timeit i in b
    
    
Determining if something is in the tuple takes about the same time as determining if something is in the list.
In [6]:
    
%timeit i in c
    
    
In [7]:
    
730e-3 / 289e-9
    
    Out[7]:
Determining if something is in the set was about 2.5 million times faster than determining if something is in the list.
In [8]:
    
%timeit i in d
    
    
In [9]:
    
730e-3 / 295e-9
    
    Out[9]:
Determining if something is a key in the dictionary was almost 2.5 million times faster than determining if something is in the list.