It's O(n), also check out: http://wiki.python.org/moin/TimeComplexity
Answer from Zach Kelling on Stack OverflowThis page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. However, it is generally safe to assume that they are not slower by more than a factor of O(log n)...
It's O(n), also check out: http://wiki.python.org/moin/TimeComplexity
This page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. However, it is generally safe to assume that they are not slower by more than a factor of O(log n)...
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).
python - what is the time complexity of list.index(obj) method? - Stack Overflow
algorithm - python str.index time complexity - Stack Overflow
python - Improving the time complexity of a function that returns the index of the first occurrence of an element in a list - Stack Overflow
Does pop(i) have a Time Complexity of O(n) or O(k)?
Videos
Time complexity is O(n) . Have a look at the link
http://wiki.python.org/moin/TimeComplexity
It's O(n), also check out: http://wiki.python.org/moin/TimeComplexity
This page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. However, it is generally safe to assume that they are not slower by more than a factor of O(log n)..
In that article [1] author goes through the algoritm and explains it. From article:
The function “fastsearch” is called. It is a mix between
Boyer-Moore and Horspool algorithms plus couple of neat tricks.
And from the wiki page of Boyer–Moore–Horspool algorithm [2]:
The algorithm trades space for time in order to obtain an
average-case complexity of O(N) on random text, although
it has O(MN) in the worst case, where the length of the
pattern is M and the length of the search string is N.
Hope that helps!
[1] http://www.laurentluce.com/posts/python-string-objects-implementation
[2] https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm
its a combination of a few algorithms- look at this
Python string 'in' operator implementation algorithm and time complexity
or this
http://effbot.org/zone/stringlib.htm