Python initialize 2d array with zeros

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The numpy.zeros() function returns a new array of given shape and type, with zeros. Syntax:

    numpy.zeros(shape, dtype = None, order = 'C')

    Parameters :

    shape : integer or sequence of integers
    order  : C_contiguous or F_contiguous
             C-contiguous order in memory(last index varies the fastest)
             C order means that operating row-rise on the array will be slightly quicker
             FORTRAN-contiguous order in memory (first index varies the fastest).
             F order means that column-wise operations will be faster. 
    dtype : [optional, float(byDeafult)] Data type of returned array.  

    Returns : 

    ndarray of zeros having given shape, order and datatype.

    Code 1 : 

    Python

    import numpy as geek

    b = geek.zeros(2, dtype = int)

    print("Matrix b : \n", b)

    a = geek.zeros([2, 2], dtype = int)

    print("\nMatrix a : \n", a)

    c = geek.zeros([3, 3])

    print("\nMatrix c : \n", c)

    Output : 

    Matrix b : 
     [0 0]
    
    Matrix a : 
     [[0 0]
     [0 0]]
    
    Matrix c : 
     [[ 0.  0.  0.]
     [ 0.  0.  0.]
     [ 0.  0.  0.]]

    Code 2 : Manipulating data types 

    Python

    import numpy as geek

    b = geek.zeros((2,), dtype=[('x', 'float'), ('y', 'int')])

    print(b)

    Output : 

    [(0.0, 0) (0.0, 0)]

    Reference : https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.zeros.html#numpy.zeros Note : zeros, unlike zeros and empty, does not set the array values to zero or random values respectively.Also, these codes won’t run on online IDE’s. Please run them on your systems to explore the working. This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    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:

    • Using simple multiplication 
    • Using loop
    • Using List comprehension
    • Using the In-Build method numpy.zeros() method
    • Using itertools.repeat() Function

    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

    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', '

    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 itertools

    temp_var = itertools.repeat(0,5)

    print(list(temp_var))

    Output:

    [0, 0, 0, 0, 0]

    How do you initialize a 2D array in Python with 0?

    To initialize all values to 0, just use a = [[0 for x in range(columns)] for y in range(rows)] .

    How do you initialize a 2D array to 0?

    Initialize 2D Array C++ To Zero.
    Method 1. int array[100][50] = {0}; ... .
    Output..
    Method 2. ... .
    Syntax int arr[100][100] memset( arr, 0, sizeof(arr) ) ... .
    std::memset is a standard library function. ... .
    Output..
    Method 3. ... .
    Output..

    How do you initialize an array in Python with 0?

    zeros() function is used which returns a new array of given shape and type, with zeros.

    How do you make a 2D NumPy array of zeros in Python?

    To create a multidimensional numpy array filled with zeros, we can pass a sequence of integers as the argument in zeros() function. For example, to create a 2D numpy array or matrix of 4 rows and 5 columns filled with zeros, pass (4, 5) as argument in the zeros function.