Python "in" operator speed

Anshu Dwibhashi picture Anshu Dwibhashi · Nov 27, 2013 · Viewed 9.9k times · Source

Is the in operator's speed in python proportional to the length of the iterable?

So,

len(x) #10
if(a in x): #lets say this takes time A
    pass

len(y) #10000
if(a in y): #lets say this takes time B
    pass

Is A > B?

Answer

lennon310 picture lennon310 · Nov 27, 2013

A summary for in:

list - Average: O(n)
set/dict - Average: O(1), Worst: O(n)

See this for more details.