How to create an array of zeros in Python?
Last Updated :
26 Apr, 2025
Our task is to create an array of zeros in Python. This can be achieved using various methods, such as numpy.zeros(), which is efficient for handling large datasets. Other different methods are:
- Using the In-Build method numpy.zeros() method
- Using simple multiplication
- Using List comprehension
- Using itertools.repeat() Function
- Using the bytearray
- Using loop
Using numpy.zeros() method
In this example, we are creating a NumPy array with zeros as the numpy.zeros() function is used which returns a new array of given shape and type, with zeros.
Python
import numpy as np
arr = np.zeros((2, 2, 3), dtype=[('x', 'int'), ('y', 'float')])
print(arr)
print(arr.dtype)
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.)]]]
[('x', '<i8'), ('y', '<f8')]
Explanation: This method uses the numpy.zeros() function to create a multi-dimensional array of zeros. In this case, it creates a 3D array with shape (2, 2, 3) and a custom structured data type (dtype) consisting of two fields: x (integer) and y (float). The numpy.zeros() function is highly efficient for handling large arrays and is ideal for numerical computations.
Using simple multiplication
In this example, we are multiplying the array of zero to 9. In return, we will get an array with 9 elements of 0’s.
Python
arr = [0] * 9
print (arr)
Output[0, 0, 0, 0, 0, 0, 0, 0, 0]
Explanation: This approach creates a list of 9 zeros by multiplying [0] with the desired length (9). It’s a simple and fast way to generate a list with a fixed number of zeros. This method works well for small datasets and lists.
Using List comprehension
Example 1: Creating 1D array using list comprehension
As we know here for loop will generate such a list, and we iterate zero within the list till the given range.
Python
arr = [0 for element in range(5)]
print(arr)
Explanation: This method creates a list of zeros using list comprehension. It iterates over a range of 5 elements, adding 0 to the list for each element. This is an efficient and Pythonic way to create lists, offering better performance than using explicit loops in most cas
Example 2: Creating 2D array using list comprehension
In this example, we are creating a 2-D array using a list comprehension in python to create 0’s of 5 rows and 10 columns.
Python
arr = [[0 for col in range(5)] for row in range(10)]
print(arr)
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 is a nested list comprehension that generates a 2D list (matrix) of zeros with 10 rows and 5 columns. It efficiently creates a list of lists, where each inner list is filled with zeros. This method is great for creating multi-dimensional arrays.
Itertools module is a memory-efficient tool that is used either by itself or in combination to form iterator algebra. Here we create an iterator using repeat() function, this function takes the value as its first argument, and then the second argument take the number of repetition of the values. Below the example, we take 0 as a value and the second parameter 5 is the number of repetitions and then convert “t” into list.
Python
import itertools
t = itertools.repeat(0,5)
print(list(t))
Explanation: This method uses the itertools.repeat() function to create an iterator that repeats 0 for 5 times. By converting it to a list using list(), we get a list of 5 zeros. This method is memory-efficient as it doesn’t create the entire list until needed but may be less intuitive than other methods.
Using the bytearray
To create an array of zeros using the bytearray approach. This will create a bytearray of 10 elements, all initialized to 0. You can then print the bytearray to see the list of zeros.
Python
arr = bytearray(10)
z = [int(str(x)) for x in arr]
print(z)
Output[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Explanation: This method creates a bytearray of 10 bytes, initialized with zeros. Then, it converts each byte to an integer and creates a list. While this is an efficient method in terms of memory, it’s less commonly used for general-purpose array creation, as bytearray represents binary data rather than typical numerical data.
Using a loop
In this example, we are creating a 0’s array using a for loop of range from 0 to 10.
Python
arr = []
for i in range(0,10):
arr.append(0)
print(arr)
Output[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Explanation: This method manually creates a list of 10 zeros by appending 0 to the list inside a loop. While functional, this method is less efficient compared to list comprehension and other techniques, as it involves repeatedly calling append() which has some overhead.
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
How to Create a Python 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. [GFGTABS] Python # Size of the list n = 5 # Creating a lis
2 min read
How to create an empty class in Python?
A class is a user-defined blueprint or prototype from which objects are created. Class can be considered as a user-defined data type. Generally, a class contains data members known as attributes of the class and member functions that are used to modify the attributes of the class. But have you ever
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
How to Create a Sparse Matrix in Python
If most of the elements of the matrix have 0 value, then it is called a sparse matrix. The two major benefits of using sparse matrix instead of a simple matrix are: Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.Computing time:
3 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 chara
2 min read
Creating a List of Sets in Python
Creating a list of sets in Python involves storing multiple sets within list structure. Each set can contain unique elements and the list can hold as many sets as needed. In this article, we will see how to create a list of set using different methods in Python. Using List ComprehensionList comprehe
2 min read
Creating Sets of Tuples in Python
Tuples are an essential data structure in Python, providing a way to store ordered and immutable sequences of elements. When combined with sets, which are unordered collections of unique elements, you can create powerful and efficient data structures for various applications. In this article, we wil
3 min read
Convert list to Python array - Python
We can use a number of approaches to convert a list into a Python array based on the requirements. One option is to use the array module, which allows us to create arrays with a specified data type. Another option is to use the numpy.array() method, which provides more features for working with arra
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