Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to create an array of zeros in Python?

How to create an array of zeros in Python?

In this article, we will discuss how to create an array of zeros in Python. In array items are stored at contiguous memory locations and here we will try to add only Zeros into the array with different methods.

Here we will cover different approaches to creating a zero’s element array. The different approaches that we will cover in this article are:

Method 1: 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.

Python3




arr0 = [0] * 9
print (arr0)


Output:

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

Method 2: Using a loop

In this example, we are creating a 0’s array using a for loop of range from 0 to 10.

Python3




arr1 = []
for i in range(0,10):
    arr1.append(0)
     
print(arr1)


Output:

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

Method 3: 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.

Python3




arr = [0 for element in range(5)]
print(arr)


Output:

[0, 0, 0, 0, 0]

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.

Python3




arr2 = [[0 for col in range(5)] for row in range(10)]
print(arr2)


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

Method 4: Using the In-Build method 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. 

Python3




import numpy as np
 
# Specifying array as a tuple, and
# Specify their data types.
arr3 = np.zeros((2, 2, 3), dtype=[('x', 'int'), ('y', 'float')])
print(arr3)
print(arr3.dtype)


Output:

In the output, i4 specifies 4 bytes of integer data type, whereas f8 specifies 8 bytes of float data type.

[[[(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', '<i4'), ('y', '<f8')]

Method 5: 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 the temp_var into list.

Python3




# import module
import itertools
 
# create instance with repeat()
# value = 0 and range = 5
temp_var = itertools.repeat(0,5)
print(list(temp_var))


Output:

[0, 0, 0, 0, 0]

Method 6: 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. 

Python3




zeros_array = bytearray(10)
zeros = [int(str(x)) for x in zeros_array]
print(zeros)


Output

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
RELATED ARTICLES

Most Popular

Recent Comments