Create a 2D Numpy Array and Accessing elements Python
Last Updated On
Friday 21st Jan 2022
Import the libraries
import numpy as np
import matplotlib.pyplot as plt
Create a list
a = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
list to a Numpy Array
arr = np.array(a)
print(arr)
[
[11, 12, 13],
[21, 22, 23],
[31, 32, 33]
]
numpy array dimensions
numpy array shape
print(arr.shape)
# (3, 3)
numpy array size
Accessing different elements of a Numpy Array
- We use the square brackets and the indices corresponding to the element we would like.
Access the element on the first row and first column
We can also use slicing in numpy arrays
- Access the element on the first row and first and second columns
print(arr[0][0:2])
# [11, 12]