Today in class, we learned that retrieving an element from a list is O(1)
in Python. Why is this the case? Suppose I have a list of four items, for example:
li = ["perry", 1, 23.5, "s"]
These items have different sizes in memory. And so it is not possible to take the memory location of li[0]
and add three times the size of each element to get the memory location of li[3]
. So how does the interpreter know where li[3]
is without having to traverse the list in order to retrieve the element?