How efficient/fast is Python's 'in'? (Time Complexity wise)

后端 未结 2 1780
猫巷女王i
猫巷女王i 2020-12-07 22:38

In Python, what is the efficiency of the in keyword, such as in:

a = [1, 2, 3]
if 4 in a:
  ...
2条回答
  •  生来不讨喜
    2020-12-07 23:12

    The complexity for lists is:

    O(n)
    

    For sets it is:

    O(1)
    

    http://wiki.python.org/moin/TimeComplexity

提交回复
热议问题