Ads by ProfitSence
Close

Create a 2D Numpy Array and Accessing elements Python

Last Updated on Wednesday 5th Oct 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

			
					print(arr.ndim)
# 2

			
	

numpy array shape

			
					print(arr.shape)
# (3, 3)

			
	

numpy array size

			
					print(arr.size)
# 9

			
	

Accessing different elements of a Numpy Array

  • We use the square brackets and the indices corresponding to the element we would like.
			
					print(arr[1, 2])
# 23

			
	

Access the element on the first row and first column

			
					print(arr[0][0])
# 11

			
	

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]