8

I would like to get some understanding on how the data types in python - list, tuple, dict and set - are implemented

How are they implemented, importantly the data structure used. Any place/ url to precisely get this understanding?

Bach
  • 6,145
  • 7
  • 36
  • 61
Raghav
  • 145
  • 1
  • 5
  • 5
    [Source code](http://hg.python.org/cpython/) would be my first preference. – thefourtheye Feb 19 '14 at 07:27
  • 4
    Implemented in Python, Jython, PyPy, ... ? – Matthias Feb 19 '14 at 07:31
  • The underlying implementations are _implementation details_ and you cannot assume they will be the same across all Python implementations. – lanzz Feb 19 '14 at 08:15
  • I am sorry about the ambiguity I am interested to know about the reference implementation CPython. However does it differ with the other implementations? I am interested to know the datastructure rather than the native data type used in the implementations. For eg: is 'dict' implemented using a hash map. why. this kind of information. Thank you – Raghav Feb 19 '14 at 08:23
  • I'd agree with @thefourtheye. The CPython source is delightfully well-commented: check `dictobject.c`, `listobject.c`, `setobject.c` and `tupleobject.c` at http://hg.python.org/cpython/file/2e8a142dbccc/Objects – lanzz Feb 19 '14 at 08:51
  • 4
    Voting down on this seems harsh. – Bach Feb 19 '14 at 10:13
  • [How is Python's List Implemented?](http://stackoverflow.com/questions/3917574/how-is-pythons-list-implemented) – bain Jul 02 '16 at 12:45

1 Answers1

8

The best place to look is the CPython implementation source code:

  • dict - Hash map targeting fast resolution of keys
  • list - Looks like an array of PyObjects
  • tuple - Same as list but with optimisations that a tuple can allow (fixed size, objects)
  • set - Hash map with optimisations for cache locality

The source code is heavily commented and well written C. This would be the best place to understand the data structures used in detail.

Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
  • thank you! I tried searching for something like this and did not get a good result. Thank you. – Raghav Feb 19 '14 at 13:15