Complexity of list.index(x) in Python

后端 未结 6 1161
礼貌的吻别
礼貌的吻别 2020-12-03 04:27

I\'m referring to this: http://docs.python.org/tutorial/datastructures.html

What would be the running time of list.index(x) function in terms of big O n

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 05:07

    According to said documentation:

    list.index(x)
    

    Return the index in the list of the first item whose value is x. It is an error if there is no such item.

    Which implies searching. You're effectively doing x in s but rather than returning True or False you're returning the index of x. As such, I'd go with the listed time complexity of O(n).

提交回复
热议问题