Create a List of Zeros in Python

Create a list of zeros in Python with [0] * n, and avoid the shared-reference trap when multiplying lists of mutable objects.

Create a list of zeros with list multiplication:

zeros = [0] * 5
print(zeros)
# [0, 0, 0, 0, 0]

[0] * n repeats the integer 0 exactly n times. Change n to get a different length:

ten_zeros = [0] * 10
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

This works for any immutable value—integers, floats, strings, None, booleans and tuples of immutables:

[""] * 3       # ['', '', '']
[None] * 4     # [None, None, None, None]
[1] * 5        # [1, 1, 1, 1, 1]
[0.0] * 3      # [0.0, 0.0, 0.0]
[(0, 0)] * 2   # [(0, 0), (0, 0)]

You can also repeat a short pattern:

[0, ""] * 2
# [0, '', 0, '']

Do not multiply lists of mutable objects

List multiplication copies references, not deep values. That is fine for integers and strings. It is a trap for lists, dicts and other mutable objects:

rows = [[0]] * 3
print(rows)
# [[0], [0], [0]]

rows[0][0] = 99
print(rows)
# [[99], [99], [99]]  — every row changed

All three outer slots point at the same inner list. Changing one appears to change every copy.

Create independent mutable items with a list comprehension instead:

rows = [[0] for _ in range(3)]
rows[0][0] = 99
print(rows)
# [[99], [0], [0]]

The same rule applies to dicts and empty lists:

# Wrong — shared dict
[{} for _ in range(3)]  # correct
[{}] * 3                # incorrect (shared)

# Wrong — shared list
[[] for _ in range(3)]  # correct
[[]] * 3                # incorrect (shared)

For more on unexpected shared references, see list changes unexpectedly in Python.

Two-dimensional list of zeros

A grid of zeros needs a nested comprehension so each row is a new list:

rows, cols = 3, 4
grid = [[0] * cols for _ in range(rows)]
print(grid)
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

grid[0][0] = 1
print(grid)
# [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Avoid [[0] * cols] * rows. The outer multiplication reuses one row object, so editing one row edits every row.

Inner multiplication with 0 is safe because integers are immutable. The outer loop must still create a new list for each row.

Alternatives

A comprehension works when the fill value is computed per index:

zeros = [0 for _ in range(5)]

For a fixed fill value, [0] * n is clearer and usually faster. itertools.repeat is another option when you want an iterator rather than a list—see creating a list of identical elements.

For a list that starts empty and grows later, start with [] and use append, or pre-size with zeros when you need random-access writes by index.

Quick reference

GoalPattern
List of zeros[0] * n
List of identical immutables[value] * n
List of independent mutables[mutable() for _ in range(n)]
2D grid of zeros[[0] * cols for _ in range(rows)]
Avoid[[0]] * n, [{}] * n, [[]] * n

To build lists with mixed starter values rather than one repeated fill, see create a list with values in Python.