Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the underlying data structure for Python lists?

What is the typical underlying data structure used to implement Python's built-in list data type?

like image 868
Nixuz Avatar asked May 27 '09 06:05

Nixuz


People also ask

What data structure is used in list?

A list is an ordered data structure with elements separated by a comma and enclosed within square brackets. For example, list1 and list2 shown below contains a single type of data. Here, list1 has integers while list2 has strings.

What is data structure in Python?

Data structures are the fundamental constructs around which you build your programs. Each data structure provides a particular way of organizing data so it can be accessed efficiently, depending on your use case. Python ships with an extensive set of data structures in its standard library.

Is list a linear data structure in Python?

Linked List. Linked lists are linear Data Structures which are not stored consequently but are linked with each other using pointers.

What is list data type in Python?

List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.


2 Answers

List objects are implemented as arrays. They are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation.

See also: http://docs.python.org/library/collections.html#collections.deque

Btw, I find it interesting that the Python tutorial on data structures recommends using pop(0) to simulate a queue but does not mention O(n) or the deque option.

http://docs.python.org/tutorial/datastructures.html#using-lists-as-queues

like image 186
e1i45 Avatar answered Sep 28 '22 16:09

e1i45


CPython:

typedef struct {     PyObject_VAR_HEAD     /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */     PyObject **ob_item;      /* ob_item contains space for 'allocated' elements.  The number      * currently in use is ob_size.      * Invariants:      *     0 <= ob_size <= allocated      *     len(list) == ob_size      *     ob_item == NULL implies ob_size == allocated == 0      * list.sort() temporarily sets allocated to -1 to detect mutations.      *      * Items must normally not be NULL, except during construction when      * the list is not yet visible outside the function that builds it.      */     Py_ssize_t allocated; } PyListObject; 

As can be seen on the following line, the list is declared as an array of pointers to PyObjects.

PyObject **ob_item; 
like image 31
Georg Schölly Avatar answered Sep 28 '22 14:09

Georg Schölly