Open In App

How to create an array of zeros in Python?

Last Updated : 12 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating an array of zeros in Python involves generating a collection filled entirely with zero values. For example, if we need to create a list of 5 zeros, the list would look like [0, 0, 0, 0, 0]. Let’s explore some common approaches to create zero-filled arrays.

Using numpy.zeros()

numpy.zeros() creates an array of zeros with any shape and supports multi-dimensional arrays. It’s very fast and memory-efficient, ideal for large numeric datasets and scientific computing. You can specify the shape and data type easily.

Python
import numpy as np

a = np.zeros((1000, 1000))
print(a)

Output
[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]

Explanation: This code creates a 2D NumPy array of shape (1000, 1000), filled with zeros.

Using simple mutiplication

This method creates a 1D list of zeros by multiplying [0] by the desired length. It’s simple, fast and works well for small to medium-sized lists. However, it’s limited to one-dimensional arrays.

Python
a = [0] * 1000
print(a)

Output
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...

Explanation: This code creates a list of 1000 zeros by multiplying [0] with 1000.

Using itertools.repeat()

itertools.repeat() generates an iterator that produces the same value repeatedly. When converted to a list, it creates a zero-filled list efficiently. It’s memory-friendly and useful for large sequences or when lazy evaluation is needed.

Python
import itertools

a = list(itertools.repeat(0, 1000))
print(a)

Output
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...

Explanation: This code uses itertools.repeat() to create an iterator that produces 0 repeated 1000 times. Converting it to a list generates a list of 1000 zeros efficiently.

Using list comprehension

List comprehension builds lists by looping through a range and inserting zeros. It is very readable and flexible, supporting both 1D and nested multi-dimensional lists. Slightly slower than multiplication but great for complex structures.

Example 1: Here, we create a one-dimensional list containing 1000 zeros.

Python
a = [0 for _ in range(1000)]
print(a)

Output
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...

Explanation: List comprehension create a list of 1000 zeros by iterating 1000 times and adding 0 in each iteration.

Example 2: Here, we build a two-dimensional list with 10 rows and 5 columns, filled with zeros.

Python
a = [[0 for _ in range(5)] for _ in range(10)]
print(a)

Output
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Explanation: This code creates a 2D list (10 rows and 5 columns) filled with zeros using nested list comprehensions. The outer loop runs 10 times for rows, and the inner loop runs 5 times for columns, producing a matrix of zeros.

Using bytearray

bytearray creates a mutable array of bytes initialized to zero. It’s very memory efficient and good for binary data or large zero sequences. You can convert it to a list of integers if needed.

Python
a = bytearray(1000)
z = list(a)
print(z)

Output
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...

Explanation: This code creates a bytearray of 1000 bytes, all initialized to zero. Converting it to a list produces a list of 1000 zeros.


Next Article
Article Tags :
Practice Tags :

Similar Reads