The Python Oracle

Why is a list access O(1) in Python?

This video explains
Why is a list access O(1) in Python?

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: A Thousand Exotic Places Looping v001

--

Chapters
00:00 Question
01:08 Accepted answer (Score 34)
01:48 Answer 2 (Score 24)
02:37 Answer 3 (Score 9)
03:26 Answer 4 (Score -2)
03:41 Thank you

--

Full question
https://stackoverflow.com/questions/3735...

Question links:
[this document]: https://wiki.python.org/moin/TimeComplex...
[this answer]: https://stackoverflow.com/questions/5138...

Answer 1 links:
[http://effbot.org/pyfaq/how-are-lists-im...]: http://effbot.org/pyfaq/how-are-lists-im...

Answer 2 links:
[amit]: https://stackoverflow.com/users/572670/a...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #list #dictionary #datastructures #timecomplexity

#avk47



ACCEPTED ANSWER

Score 41


Get item is getting an item in a specific index, while lookup means searching if some element exists in the list. To do so, unless the list is sorted, you will need to iterate all elements, and have O(n) Get Item operations, which leads to O(n) lookup.

A dictionary is maintaining a smart data structure (hash table) under the hood, so you will not need to query O(n) times to find if the element exists, but a constant number of times (average case), leading to O(1) lookup.




ANSWER 2

Score 33


Accessing a list l at index n l[n] is O(1) because it is not implemented as a Vanilla linked list where one needs to jump between pointers (value, next-->) n times to reach cell index n.

If the memory is continuous and the entry size would had been fixed, reaching a specific entry would be trivial as we know to jump n times an entry size (like classic arrays in C).

However, since list is variable in entries size, the python implementation uses a continuous memory list just for the pointers to the values. This makes indexing a list (l[n]) an operation whose cost is independent of the size of the list or the value of the index.

For more information see: FAQ, variable-length array (VLA) & Dynamic array




ANSWER 3

Score 9


That is because Python stores the address of each node of a list into a separate array. When we want an element at nth node, all we have to do is look up the nth element of the address array which gives us the address of nth node of the list by which we can get the value of that node in O(1) complexity.

Python does some neat tricks to make these arrays expandable as the list grows. Thus we are getting the flexibility of lists and and the speed of arrays. The trade off here is that the compiler has to reallocate memory for the address array whenever the list grows to a certain extent.

amit has explained in his answer to this question about why lookups in dictionaries are faster than in lists.