Reputation: 4842
I have a dictionary with 1,000,000
keys, I want to check if a certain key exists in a dictionary.
I could also have 1,000,000
items in a list which would hold all of those keys.
Which is the faster way and why?
For example:
999999 in {x:x for x in range(1000000)}
vs
999999 in [x for x in range(1000000)]
Upvotes: 0
Views: 83
Reputation: 572
Dictionaries are faster because keys are hashed. See here: https://wiki.python.org/moin/TimeComplexity
Upvotes: 5