Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn't good enough to look at the source code.
This is implementation dependent, but IIRC:
- CPython uses an array of pointers
- Jython uses an
ArrayList
- IronPython apparently also uses an array. You can browse the source code to find out.
Thus they all have O(1) random access.
In CPython, lists are arrays of pointers. Other implementations of Python may choose to store them in different ways.
It's an array. Practical proof: Indexing takes (of course with extremely small differences (0.0013 µsecs!)) the same time regardless of index:
...>python -m timeit --setup="x = [None]*1000" "x[500]"
10000000 loops, best of 3: 0.0579 usec per loop
...>python -m timeit --setup="x = [None]*1000" "x[0]"
10000000 loops, best of 3: 0.0566 usec per loop
I would be astounded if IronPython or Jython used linked lists - they would ruin the performance of many many widely-used libraries built on the assumption that lists are arrays.
The C code is pretty simple, actually. Expanding one macro and pruning some irrelevant comments, the basic structure is in listobject.h
, which defines a list as:
typedef struct {
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;
PyObject_HEAD
contains a reference count and a type identifier. So, it's a vector/array with pre-allocation.