Search code examples
pythontimesizein-operator

Python "in" operator speed


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?


Solution

  • A summary for in:

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

    See this for more details.