Say I have millions of string IDs, I want to store them in a variable and check if one ID exists, there are both ways I can think of, list
and dict
:
Using list
>>> timeit_a = timeit.Timer('"9999999" in a', setup='a = [str(i) for i in range(3000000)]')
>>> timeit_a.timeit(1)
0.06293477199994868
Using dict
>>> timeit_b = timeit.Timer('"9999999" in b', setup='b = {str(i): None for i in range(3000000)}')
>>> timeit_b.timeit(1)
3.860999981952773e-06 # equal to 0.00000386099
As we can see using dict
is much much much faster, but I feel creating the dict
with bunch of None
s for the sake of just utilizing the hashmap of keys is not very elegant.
Is there a more canonical and more elegant way to do it?