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
Does pop(i) have a Time Complexity of O(n) or O(k)?
Python List Indexing Efficiency - Stack Overflow
python - What is (list.index() inside a for loop) complexity - Stack Overflow
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)..
Python does not iterate through lists to find a particular index. Lists are arrays (of pointers to elements) in contiguous memory and so locating the desired element is always a simple multiplication and addition. If anything, list[-1] will be slightly slower because Python needs to add the negative index to the length to get the real index. (I doubt it is noticeably slower, however, because all that's done in C anyway.)
Why not test and see?
import timeit
t=timeit.timeit('mylist[99]',setup='mylist=list(range(100))',number=10000000)
print (t)
t=timeit.timeit('mylist[-1]',setup='mylist=list(range(100))',number=10000000)
print (t)
Of course, as you can see by running this a couple times, there's really no (noticable) difference for the reasons pointed out in the other answers.
The problem you have in your current methods is that you make placing one element dependent on the entire opposing list. Instead, simply make a chart of where each element is in each list.
Use a dict. Go through list1, filling in key:value to be the element and its index. This gives you
{ 5: 0, 8: 1, 1: 2, 4: 3 }
Now, go through list 2. Access each element; if it exists (use get for the fail-safe functionality), print the stored index (list1) and the current index (list2). If it doesn't exist, then simply skip printing.
O(N) complexity.
If this is the code:
for searched in first: # O(n)
i = second.index(searched) # O(m)
print(i)
Then the answer is O(n * m), where n = len(first), m = len(second).
It can be done in O(n + m) if you use dictionaries.
Accessing any array element is in constant time, since it is a known by memory location (i.e. a pointer.)
The array does not need to go through prior elements in order to access the nth element (i.e. it is not like a linked list.) The location of all elements is known before hand and can simply be accessed directly.
Updated thanks to comments.
array[-x] is syntactic sugar for array[len(lst) - x]. So it's still a simple constant access to a pointer and takes no time.
You can see this answer for a bit more info. While it is about C, the concepts should be the same.
Native Python lists are arrays of pointers and accessing any element is O(1).
Note that this is an implementation detail specific to the standard Python implementation, known as CPython. Other language implementations may implement lists differently.