How to create an array of zeros in Python?
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.
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.
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.
arr = [0 for element in range(5)]
print(arr)
Output
[0, 0, 0, 0, 0]
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.
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.
Using itertools.repeat() Function
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.
import itertools
t = itertools.repeat(0,5)
print(list(t))
Output
[0, 0, 0, 0, 0]
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.
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.
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.