Basically, there’re two ways to store data strctures: array and linked list. All other data strctures, such as queue, stack, graph, hashmap, tree, heap, etc, all can be implemented by array and linked list.
In python, list = array is implemented by dynamic array, when it used up the original assigned space, it will point to new sapce.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
typedefstruct { PyObject_HEAD Py_ssize_t ob_size;
/* 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 */ Py_ssize_t allocated; } PyListObject;
The array itself stores a list of of pointers. Therefore, while using list, need to cautious to some traps like: during initialization, all pointers are pointing to the same list