6

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 4 items, i.e:

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 3 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 search of it?

Teererai Marange
  • 2,044
  • 4
  • 32
  • 50

0 Answers0