Python initialize 2d array of zeros

7939

In this article, we will discuss How to create and initialize a 2D array or two-dimensional list of zeroes in Python. You can also implement this code using the function as well as classes.

Python Code to initialize 2d array of zeros

 

Python code implementation without user-defined functions & classes

Code: 
two_D_array_column_size = int(input("Enter size of column: "))
two_D_array_row_size = int(input("Enter size of row: "))

#Declaring an empty 1D list.
two_D_array = []
#Declaring an empty 1D list.
b_column = []

#Initialize the column to Zeroes.
for j in range(0, two_D_array_column_size):
        b_column.append(0)
#Append the column to each row.
for i in range(0, two_D_array_row_size):
    two_D_array.append(b_column)

# 2D array is created.
#Print the two dimensional list.
print(two_D_array)

 

Output:
Enter size of column: 3
Enter size of row: 2
[[0, 0, 0], [0, 0, 0]]