Skip to main content
A Python list is a built-in, mutable, ordered sequence data type that stores a collection of heterogeneous object references. Under the hood, CPython implements lists as dynamic arrays of pointers. This means a list does not store the actual objects inline, but rather stores contiguous memory addresses pointing to the objects, allowing it to hold mixed data types while maintaining O(1)O(1) time complexity for index-based access.

Instantiation and Syntax

Lists are created using square bracket literal syntax [] or the built-in list() constructor.

Core Characteristics

  • Mutable: The internal state of the list can be altered in place without creating a new object in memory. Elements can be reassigned, added, or removed.
  • Ordered: Lists maintain the exact insertion order of their elements.
  • Zero-indexed: Element access begins at index 0. Python also supports negative indexing, where -1 resolves to the final element.
  • Dynamic Sizing: Lists automatically handle memory reallocation. When the underlying array reaches its capacity, Python allocates a new, larger array (typically over-allocating by a proportional factor) and copies the existing pointers to the new memory block.

Indexing and Slicing

Lists support standard sequence operations, including slicing, which extracts a new list based on the [start:stop:step] syntax.

Mutation Operations

Because lists are dynamic arrays, operations that modify the end of the list are highly optimized, while operations that modify the beginning or middle require shifting elements in memory.

Memory and Performance Profile

Understanding the time complexity of list operations is critical for performance optimization: Because insert() and pop(0) operate in O(n)O(n) time due to memory shifting, Python lists are inefficient when used as queues (FIFO structures). For double-ended queue operations, collections.deque is the standard library alternative, implemented as a doubly linked list.
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More