Python “in” operator speed

前端 未结 2 658
心在旅途
心在旅途 2021-01-17 15:08

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         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 15:57

    A summary for in:

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

    See this for more details.

提交回复
热议问题