HOWTO · Python
How to Create List of Zeros in Python
This tutorial demonstrates how to generate a list containing zeros in Python. Explore various methods such as list comprehension, the multiplication operator, and using the numpy library. Whether you're a beginner or an experienced programmer, this guide provides clear code examples and explanations to help you efficiently create lists of zeros for your projects.
On this page
To create a Python list containing n zeros, use [0] * n. For example, [0] * 5 makes a list with five integer zeros. This is the clearest choice when the required length is known and each item has the same value. The examples below use n = 5 so you can compare the approaches directly.
Use list multiplication for a fixed-length list of zeros
List multiplication repeats the items in the list on the left. Since [0] contains one integer, multiplying it by n produces n integer zeros:
n = 5
zeros = [0] * n
The result is a new list. A count of zero produces []; a negative count also produces [], because Python repeats a sequence zero times for a non-positive repetition count. For this example, the repeated value is an integer, so each list position can be reassigned independently. If you repeat a mutable object such as a nested list, each position refers to the same object; use a comprehension to construct distinct objects instead.
Use a list comprehension to create a list of zeros
A list comprehension evaluates its expression once for each item in an iterable. Here, range(n) supplies five iterations, and _ names a loop value that the expression does not need. The phrase list comprehension refers to this bracketed expression:
n = 5
zeros = [0 for _ in range(n)]
Use this form when the value is calculated for each iteration or when you want the generation rule to be visible. Because the loop value is unused here, _ is a conventional name; it has no special behavior in Python. You can replace 0 with an expression that depends on the iteration value when each element should be calculated differently.
Use a loop when creating each value needs custom logic
An explicit loop is useful when building each item involves several statements, a condition, or another operation. Append one value during each iteration:
n = 5
zeros = []
for _ in range(n):
zeros.append(0)
This produces the same kind of built-in list as the multiplication and comprehension examples. For the simple task of repeating an integer, the other two forms express the result more compactly; use the loop when its body makes per-item work easier to understand. In a real program, replace the append(0) statement with the work needed to calculate or prepare each value.
Use itertools.repeat() to repeat zero
itertools.repeat() supplies the same object repeatedly. Give it the value and count, then pass its iterator to list() when the caller needs a list:
from itertools import repeat
n = 5
zeros = list(repeat(0, n))
This is helpful when an iterator is useful directly or when a repeated-value iterator fits into a larger operation. The list(...) call consumes the iterator and stores all five values in memory, so [0] * n is usually simpler when the desired result is already a list.
Use NumPy when you need a numerical array
For numerical work that uses NumPy operations, numpy.zeros(n) creates a NumPy array with n zero-valued elements. For example, with NumPy installed, import numpy as np followed by zeros = np.zeros(5) creates an array. This result is an ndarray, not a built-in Python list; convert it with zeros.tolist() only when another part of your code specifically requires a list. NumPy is an additional dependency and is useful when you need its array operations, shapes, or data types. For an ordinary list, use [0] * n without adding a dependency.
Choose multiplication for a simple fixed-length list, a comprehension for an expression evaluated per item, a loop for custom steps, itertools.repeat() when its iterator is useful, or NumPy when an array is the intended data structure.