In Python, lists are dynamic arrays that grow as you append items. However, there are scenarios, such as creating a placeholder array for algorithms, setting up a grid for a game, or pre-allocating memory for performance, where you need to initialize a list with a specific length and a default value (like 0 or None).
This guide explores the standard multiplication method, the safe approach for mutable objects (like lists of lists), and high-performance alternatives.
Method 1: Using the Multiplication Operator (Standard)
The fastest and most Pythonic way to initialize a list with a constant, immutable value (like integers, floats, strings, or None) is using the multiplication operator *.
Syntax:
[default_value] * lengthExample:
length = 5
zeros = [0] * length
print(f"Zeros: {zeros}")
placeholders = [None] * length
print(f"Placeholders: {placeholders}")
strings = ["empty"] * 3
print(f"Strings: {strings}")Output:
Zeros: [0, 0, 0, 0, 0]
Placeholders: [None, None, None, None, None]
Strings: ['empty', 'empty', 'empty']Method 2: List Comprehensions (Safe for Mutables)
If you need to initialize a list where every element is a new, distinct mutable object (like a dictionary, a set, or another list), you must use a list comprehension. This ensures that the constructor is called repeatedly for each element.
Syntax:
[Expression for _ in range(length)]Example:
length = 3
matrix = [[] for _ in range(length)]
matrix[0].append(1)
print(f"Matrix: {matrix}")Output:
Matrix: [[1], [], []]The Common Pitfall: The Mutable Reference Trap
A frequent bug occurs when developers use the multiplication operator * to create multi-dimensional lists. Python copies the reference to the object, not the object itself. This means every element in the list points to the exact same object in memory.
Reproducing the Error
length = 3
matrix = [[]] * length
print(f"Initial: {matrix}")
matrix[0].append(99)
print(f"After modification: {matrix}")Output:
Initial: [[], [], []]
After modification: [[99], [99], [99]]Method 3: Initializing Sequences with Range
If you need a list initialized with a sequence of numbers (e.g., 0, 1, 2, 3...) rather than a single repeating value, use the list() constructor with range().
length = 5
sequence = list(range(length))
print(f"Sequence: {sequence}")
evens = list(range(0, 10, 2))
print(f"Evens: {evens}")Output:
Sequence: [0, 1, 2, 3, 4]
Evens: [0, 2, 4, 6, 8]Conclusion
To initialize lists with a specific length in Python:
- Use
[val] * Nfor immutable types likeint,str, orNone. It is the fastest method. - Use
[[] for _ in range(N)]for mutable types like lists or dicts to avoid shared reference bugs. - Use
list(range(N))to create sequential numerical lists.