How to Create a 2D Python array

In this post, we will learn how to Create a 2D Python array with examples by using NumPy module or without numpy module,create a 2D python numpy list compreshension,create 2D python numpy array using itertools module ,using for loop and list.append() method,using np.empty(),np.ones(),np.zeros(),np.ones()

1.Python initialize 2D array using for loop


In this python program, we have used for loop with range() function in a list comprehension. The range function generates a sequence of numbers as per given start, stop, and steps. We can initialize the 2D python array as per need.

#python program to create 2D array

rows, cols = (3, 3)
 
array = [[1 for x in range(rows)] for y in range(cols)] 
print(array)

array2 = [[1*x+y for x in range(rows)] for y in range(cols)] 
print(array2)

Output

[[1, 1, 1], [1, 1, 1], [1, 1, 1]]

[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

2. List comprehension with shorten code


In this Python program, we are using list comprehension with shortened code. It creates a 2D Python array.

#python program to create 2D array using list comprehesion

rows, cols = (3, 3)



x = [[4]*rows for _ in range(cols)]
 
for ind in range(cols):
        print(x[ind])

Output

[4, 4, 4]
[4, 4, 4]
[4, 4, 4]

3. Create 2D array using itertools module


In this Python program example we are using itertools import method that takes two arguments values we have to repeat and how many times we are repeating.

from itertools import repeat

size = 3
array = list(repeat([0], size))

print(array) 

Output

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

4. Create 2D python array


In this python program example, we have used list comprehension to declare and initialize the python array with default values 0. We will three different ways to initialize a 2D array.The expression to create a 2D array [[val]*cols]*rows

Example 2: initialize 2D python array with 0

rows, cols = (3, 3)
array = [[0]*cols]*rows
print(array)

Output

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

5. Create 2D python array using list.append() method


In this Python program example, we are using for loop to iterate over rows and an inner loop to iterate over the columns and appending to list to create the 2D array.

rows, cols = (3, 3)
array =[]
for x in range(rows):
    col = []
    for y in range(cols):
        col.append(0)
    array.append(col)
print(array)

Output

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

6. Create 2D using NumPy Module


In this python program we will use NumPy module to create 2D Numpy Array.We are using np.empty(), np.zeros(), np.full(),np.ones() to create 2D Numpy array.

np.empty() method

#create 2D numpy array

import numpy
array = numpy.empty(shape=(2,5),dtype='object')
print(array)

Output

[[None None None None None]
 [None None None None None]]

using np.zero()


The numpy. zeros() function returns a new array of given shape and type, initialized with zeros. Let us understand how will create an empty numpy array using np. zeros() function.

np.zeros() to Create 2D NumPy array

#python program to create 2D numpy array

import numpy as np

nparr = np.zeros((3,2))

print(nparr)


Output

[[0. 0.]
 [0. 0.]
 [0. 0.]]

np.full() to create 2D Numpy array

The np. full() function creates a numpy array of given shape fill all values with the same values.

import numpy as np

nparr = np.full((3,3),5)

print(nparr)


Output

[[5 5 5]
 [5 5 5]
 [5 5 5]]

np.ones() to create 2D Numpy array

The np. ones(shape) function creates a 2D numpy array of ones.

#python program to create 2D np.ones() to numpy array 
import numpy as np

nparr = np.ones((3,3))

print(nparr)

Output

[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]

Summary

In this post, we have learned how to Create a 2D Python array a numpy module without numpy modules.