How to Create an Array of Zeros in Python?

In this tutorial, I will explain how to create an array of zeros in Python. Someone asked me this doubt during a Python webinar and this was the topic of discussion. So I decided to write an article on this. We will explore various methods to create arrays filled with zeros with examples and screenshots of executed example code.

Methods to Create Arrays of Zeros in Python

There are several ways to create arrays of zeros in Python. We will cover all the methods

Check out setting an array element with a sequence error in Python

Method 1. Using List Comprehensions

List comprehensions provides an easy way to create lists in Python. This method is simple and effective for creating one-dimensional and multi-dimensional arrays of zeros.

Example: One-Dimensional Array

# Creating a one-dimensional array of zeros with 10 elements
zero_array = [0 for _ in range(10)]
print(zero_array)

Output:

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

Refers to the screenshot below, which displays the output.

Create an Array of Zeros in Python

Read Python program to print the smallest element in an array

Example: Multi-Dimensional Array

# Creating a 2D array of zeros with 3 rows and 4 columns
zero_matrix = [[0 for _ in range(4)] for _ in range(3)]
print(zero_matrix)

Output:

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

Refers to the screenshot below, which displays the output.

How to Create an Array of Zeros in Python

Check out How to Split a String into an Array in Python

Method 2. Using the bytearray Function

The bytearray function in Python can be used to create an array of zeros. This method is more useful when dealing with binary data.

Example: Bytearray of Zeros

# Creating a bytearray of zeros with 10 elements
zero_bytearray = bytearray(10)
print(list(zero_bytearray))

Output:

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

Refers to the screenshot below, which displays the output.

Create an Array of Zeros in Python Using the bytearray Function

Read 3D Arrays in Python

Method 3. Using the NumPy Library

Python NumPy is a useful library for numerical computations in Python. It provides an easy and efficient way to create arrays of zeros with its numpy.zeros() function.

Installing NumPy

If you haven’t installed NumPy, you can do so using pip:

pip install numpy

Example: One-Dimensional Array

import numpy as np

# Creating a one-dimensional array of zeros with 10 elements
zero_array = np.zeros(10)
print(zero_array)

Output:

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

Refers to the screenshot below, which displays the output.

Create an Array of Zeros in Python Using the NumPy Library

Example: Multi-Dimensional Array

import numpy as np

# Creating a 2D array of zeros with 3 rows and 4 columns
zero_matrix = np.zeros((3, 4))
print(zero_matrix)

Output:

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

Example: Specifying Data Type

You can also specify the data type of the elements in the array using the dtype parameter.

import numpy as np

# Creating a 2D array of zeros with integers
zero_matrix_int = np.zeros((3, 4), dtype=int)
print(zero_matrix_int)

Output:

[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

Check out How to Print Duplicate Elements in Array in Python

Example: Initialize a Grid for a Simulation

Let’s consider a real-world example where we need to initialize a grid for a simulation. Suppose we are simulating the spread of a wildfire in a forest. We can use a 2D array of zeros to represent the initial state of the forest, where each cell in the array represents a section of the forest.

import numpy as np

# Dimensions of the forest grid
rows = 100
cols = 100

# Initializing the forest grid with zeros
forest_grid = np.zeros((rows, cols), dtype=int)

# Printing a portion of the grid
print(forest_grid[:10, :10])

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 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]]

In this example, we created a 100×100 grid initialized with zeros. This grid can now be used to simulate the spread of the wildfire by updating the values in the array as the simulation progresses.

Read How to Convert an Array to a Tuple in Python

Conclusion

In this tutorial, I have explained how to create an array of zeros in Python and discussed various methods to create arrays of zeros in Python, like using list comprehensions which is a simple and easy method, using the bytearray function which is useful when dealing with binary data, using the NumPy library which uses numpy.zeros() function, I also gave a real-world example of Initializing a grid for a simulation of a 2D array.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.