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
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.
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.
Similar Reads
How to Create String Array in Python ? To create a string array in Python, different methods can be used based on the requirement. A list can store multiple strings easily, NumPy arrays offer more features for large-scale data and the array module provides type-restricted storage. Each method helps in managing collections of text values
2 min read
Python3 Program to Move all zeroes to end of array Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity i
4 min read
Python - Create List of Size n Creating a list of size n in Python can be done in several ways. The easiest and most efficient way to create a list of size n is by multiplying a list. This method works well if we want to fill the list with a default value, like 0 or None.Python# Size of the list n = 5 # Creating a list of size n
2 min read
How to Create a String of Specified Length in Python Creating a string of a specific length in Python can be done using various methods, depending on the type of string and its contents. Letâs explore the most common techniques to achieve this.The simplest way to create a string of a specified length is by multiplying a character or sequence of charac
2 min read
Python | Shift zeroes at end of list The conventional problem involving the element shifts has been discussed many times earlier, but sometimes we have strict constraints performing them and knowledge of any possible variation helps. This article talks about one such problem of shifting 0's at end of list, catch here is it checks for j
7 min read
How To Return 0 With Divide By Zero In Python Dividing a number by zero is a big problem in math, and it can cause errors that suddenly stop your Python program. However, what if you could smoothly deal with this issue and make your program return a specific value, such as 0, instead of crashing? This article looks into different methods to acc
3 min read