 # how to create 2D array in python 3.8

Welcome everyone, Today we will learn how to create a two-dimensional array in python so let’s start:
How do you create a two-dimensional array in Python language? how to create a two-dimensional array in python

## There are different ways to create Numpy arrays in python:

• Using special library functions
• Using Numpy functions
• Conversion from other Python structures like lists

we know that Python provides many ways to create 1-dimensional,2-dimensional lists/arrays.  so let’s start to create a 1D array of size N initialized with 1s.
learn Matplotlib for Data Visualization

### How do you declare a two-dimensional array in python language?

Method 1A)

# First method to create a 1 D array using python code
Nu = 5
arrays = *Nu
print(arrays)

Output:

[2, 2, 2, 2, 2]

Method 1B)

# Second method to create a 1 D array in python
Nu = 5
arrays = [8 for i in range(Nu)]
print(arrays)

Output:

[8, 8, 8, 8, 8]

#### we can use the below code to define 2-dimensional arrays in the following ways.

Method 2A)

# 2D array using python code
rows, cols = (3, 3)
arrays = [*cols]*rows
print(arrays)

Output:

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

Method 2B)

# second method to create 2D array in python
rows, cols = (3, 3)
arrays = [[1 for i in range(cols)] for j in range(rows)]
print(arrays)

Output:

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

Method 2C)

#Third method to create 2D array in python
rows, cols = (3, 3)
arrays=[]
for i in range(cols):
col = []
for j in range(rows):
col.append(1)
arr.append(col)
print(arrays)

Output:

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

Summary: