0
0
PythonHow-ToBeginner · 4 min read

How Python List Works Internally: Explanation and Examples

A Python list is a dynamic array that stores references to objects in a contiguous block of memory. Internally, it manages capacity and resizes by allocating a larger block when needed, copying existing references to the new space to allow efficient appends.
📐

Syntax

A Python list is created using square brackets [] or the list() constructor. It can hold items of any type, and its size can change dynamically as you add or remove elements.

  • my_list = []: creates an empty list.
  • my_list = [1, 2, 3]: creates a list with initial elements.
  • my_list.append(item): adds an item to the end.
python
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
Output
[1, 2, 3, 4]
💻

Example

This example shows how a Python list grows internally when you add elements. The list starts empty and appends numbers one by one. Internally, Python allocates extra space to avoid resizing on every append, making appends efficient.

python
my_list = []
import sys

for i in range(10):
    my_list.append(i)
    print(f"Length: {len(my_list)}, Size in bytes: {sys.getsizeof(my_list)}")
Output
Length: 1, Size in bytes: 72 Length: 2, Size in bytes: 104 Length: 3, Size in bytes: 104 Length: 4, Size in bytes: 104 Length: 5, Size in bytes: 136 Length: 6, Size in bytes: 136 Length: 7, Size in bytes: 136 Length: 8, Size in bytes: 136 Length: 9, Size in bytes: 200 Length: 10, Size in bytes: 200
⚠️

Common Pitfalls

One common mistake is assuming Python lists store the actual data directly. Instead, they store references (pointers) to objects, so modifying mutable objects inside a list affects all references.

Another pitfall is inefficiently growing lists by concatenation in a loop, which creates new lists each time and slows down performance.

python
my_list = [[1, 2], [3, 4]]
my_list[0].append(3)
print(my_list)  # Both references show the change

# Inefficient way:
result = []
for i in range(5):
    result = result + [i]  # Creates new list each time
print(result)

# Efficient way:
result = []
for i in range(5):
    result.append(i)
print(result)
Output
[[1, 2, 3], [3, 4]] [0, 1, 2, 3, 4] [0, 1, 2, 3, 4]
📊

Quick Reference

Python list internal behavior summary:

  • Lists store references to objects, not the objects themselves.
  • They use a dynamic array with extra capacity to optimize appends.
  • When capacity is exceeded, lists resize by allocating a larger block and copying references.
  • Appending is usually O(1) amortized time due to resizing strategy.
  • Modifying mutable objects inside lists affects all references to those objects.

Key Takeaways

Python lists are dynamic arrays storing references to objects in contiguous memory.
Lists allocate extra space to minimize resizing and keep appends efficient.
Appending by concatenation in loops is inefficient; use append() instead.
Lists store references, so modifying mutable objects inside affects all references.
Resizing copies references to a new larger memory block when capacity is exceeded.