NumPy Array (Index of elements)

1-dimensional array :

Example : ( 1-dimensional array - Showing the value of each index)

In [41]:
import numpy as np

arr1 = np.array([1, 2, 3, 4])

print("This array : " , arr1)

#printing the element with index 0 in this array
print("\nValue of index 0 is : " , arr1[0])

#printing the element with index 1 in this array
print("\nValue of index 0 is : " , arr1[1])

#printing the element with index 2 in this array
print("\nValue of index 0 is : " , arr1[2])

#printing the element with index 3 in this array
print("\nValue of index 0 is : " , arr1[3])
This array :  [1 2 3 4]

Value of index 0 is :  1

Value of index 0 is :  2

Value of index 0 is :  3

Value of index 0 is :  4

Example : ( 1-dimensional array - Sum of the value of indexes)

In [42]:
import numpy as np

arr1 = np.array([1, 2, 3, 4])

print("This array : " , arr1)


print("\nValue of index 2 is : " , arr1[2])

print("\nValue of index 3 is : " , arr1[3])

print("\nSum of the value of index 2 and 3 is : " , arr1[2] + arr1[3])
This array :  [1 2 3 4]

Value of index 2 is :  3

Value of index 3 is :  4

Sum of the value of index 2 and 3 is :  7

2-dimensional array :

Example : ( 2-dimensional array - Showing each dimension)

In [43]:
import numpy as np

arr2 = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print("This array : " , arr2)

print('\nFisrt dimension of this array is', arr2[0])

print('\nSecond dimension of this array is', arr2[1])
This array :  [[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

Fisrt dimension of this array is [1 2 3 4 5]

Second dimension of this array is [ 6  7  8  9 10]
In [44]:
import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(arr[0, 1, 2])
6

Example : ( 2-dimensional array - Showing the value of each index for first dimension)

In [45]:
import numpy as np

arr2 = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print("This array : " , arr2)

print('\nFisrt dimension of this array is', arr2[0])

print('\nThe value of index 0 in first dimension is : ', arr2[0,0])

print('\nThe value of index 1 in first dimension is : ', arr2[0,1])
This array :  [[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

Fisrt dimension of this array is [1 2 3 4 5]

The value of index 0 in first dimension is :  1

The value of index 1 in first dimension is :  2

Example : ( 2-dimensional array - Showing the value of each index for second dimension)

In [46]:
import numpy as np

arr2 = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print("This array : " , arr2)

print('\nSecond dimension of this array is', arr2[1])

print('\nThe value of index 0 in second dimension is : ', arr2[1,0])

print('\nThe value of index 1 in second dimension is : ', arr2[1,1])
This array :  [[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

Second dimension of this array is [ 6  7  8  9 10]

The value of index 0 in second dimension is :  6

The value of index 1 in second dimension is :  7

3-dimensional array :

Example : ( 3-dimensional array - Showing each dimension)

In [47]:
import numpy as np

arr2 = np.array([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]])

print("This array : " , arr2)

print('\nFisrt dimension of this array is', arr2[0])

print('\nSecond dimension of this array is', arr2[1])

print('\nThird dimension of this array is', arr2[2])
This array :  [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]

Fisrt dimension of this array is [1 2 3 4 5]

Second dimension of this array is [ 6  7  8  9 10]

Third dimension of this array is [11 12 13 14 15]

Example : ( 3-dimensional array - Showing the value of each index for each dimension)

In [48]:
import numpy as np

arr2 = np.array([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]])

print("This array : " , arr2)


print('\nFisrt dimension of this array is', arr2[0])

print('The value of index 0 in first dimension is : ', arr2[0,0])

print('The value of index 1 in first dimension is : ', arr2[0,1])


print('\nSecond dimension of this array is', arr2[1])

print('The value of index 0 in second dimension is : ', arr2[1,0])

print('The value of index 1 in second dimension is : ', arr2[1,1])


print('\nThird dimension of this array is', arr2[2])

print('The value of index 0 in third dimension is : ', arr2[2,0])

print('The value of index 1 in third dimension is : ', arr2[2,1])
This array :  [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]

Fisrt dimension of this array is [1 2 3 4 5]
The value of index 0 in first dimension is :  1
The value of index 1 in first dimension is :  2

Second dimension of this array is [ 6  7  8  9 10]
The value of index 0 in second dimension is :  6
The value of index 1 in second dimension is :  7

Third dimension of this array is [11 12 13 14 15]
The value of index 0 in third dimension is :  11
The value of index 1 in third dimension is :  12

Negative Index

Example : Printing last element of each dimension

In [52]:
import numpy as np

arr2 = np.array([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]])

print("This array : " , arr2)


print('\nFisrt dimension of this array is', arr2[0])

print('The value of last element in first dimension is : ', arr2[0,-1])



print('\nSecond dimension of this array is', arr2[1])

print('The value of last element in second dimension is : ', arr2[1,-1])


print('\nThird dimension of this array is', arr2[2])

print('The value of last element in third dimension is : ', arr2[2,-1])
This array :  [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]

Fisrt dimension of this array is [1 2 3 4 5]
The value of last element in first dimension is :  5

Second dimension of this array is [ 6  7  8  9 10]
The value of last element in second dimension is :  10

Third dimension of this array is [11 12 13 14 15]
The value of last element in third dimension is :  15