Jonas Palačionis
Jonas Palačionis

Reputation: 4842

Is finding a key in a dictionary takes the same time as finding an item in a list

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

Answers (1)

John Titor
John Titor

Reputation: 572

Dictionaries are faster because keys are hashed. See here: https://wiki.python.org/moin/TimeComplexity

Upvotes: 5

Related Questions