Python NumPy Array Indexing Examples

Python NumPy array indexing is used to access values in the 1-dimensional and, multi-dimensional arrays. Indexing is an operation, use this feature to get a selected set of values from a NumPy array. Note that the index in ndarray starts from zero hence, there is a difference between the value and where the value is located in an array.

In this article, I will explain how to get the values from an array by using its indexes.

1. Quick Examples of NumPy Array Indexing

If you are in a hurry, below are some quick examples of how to get Python NumPy array indexing.


# Below are a quick examples

# Example 1: Use numpy.array() to get first element
arr = np.array([2, 4, 6, 8])
print(arr[0])

# Example 2: Access 2 Dimensional Arrays Indexing
arr = np.array([[0,3,5,7,9], [11,13,15,17,19]])
print('3rd element on 1st row: ', arr[0, 2])

# Example 3: Access 3 Dimensional Arrays Index
arr = np.array([[[0,3,5,], [7,9,11]], [[13, 15, 17], [19, 21, 23]]])
print(arr[0, 1, 2])

# Example 4: Use Negative Array Index
arr = np.array([[0,3,5,7,9],[11,13,15,17,19]])
print('Last element from 1st dimensional: ', arr[0, -1])
print('Last element from 2nd dimensional: ', arr[1, -1])

2. Get the 1-Dimensional NumPy Array Values Using Indexing

ndarrays is indexed using the standard Python x[obj] syntax, where x is the array and obj is the selection. You can access an array value by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, the second has index 1, etc.

In one-dimensional arrays, values are stored individually, so we can access those values by using their indices. Because every value in arrays has its own index. For example,

Another way to access elements from NumPy array using NumPy array slicing.

Get the first value of the array using array indexing.


# import numpy module
import numpy as np

# Use NumPy.array() to get first value
arr = np.array([2, 4, 6, 8])
print(arr[0])

# Output
# 2

From the above, arr contains four values 24,6,and 8. Each of these values has its different indices.

Get the third and fourth values of the array using indexing and apply addition.


# Use NumPy.array() to get third and fourth elements
arr = np.array([2, 4, 6, 8])
print(arr[2] + arr[3])

# Output
#   14

3. Get the 2-Dimensional Array Values Using Indexing

Use array indexing to access 2-dimensional array values, you can use comma-separated integers representing the dimension and the index of the element. Two-dimensional arrays are like a table with rows and columns, where the row represents the dimension and the index represents the column. Follow the below examples to get the third column value on the first row.


# Use array Indexing to get
# the values of two-dimensional arrays
arr = np.array([[0,3,5,7,9], [11,13,15,17,19]])
print('3rd value on 1st row: ', arr[0, 2])

# Output
# 3rd value on 1st row:  5

Get the 4th column value on the 2nd row.


# Get the values of two Dimensional Arrays using indexing
print('4th element on 2nd row: ', arr[1, 3])

# Output
# 4th element on 2nd row:  17

4. Get the 3-Dimensional Array Values Using Array Indexing

Use NumPy array indexing to access 3-dimensional array values, you can use comma-separated integers representing the dimension and the index of the element. Follow the below examples to get the third element of the second array of the first array.


# Access values of 3-Dimensional arrays using index
arr = np.array([[[0,3,5,], [7,9,11]], [[13, 15, 17], [19, 21, 23]]])
print(arr[0, 1, 2])

# Output
# 11

5. Use Negative Array Indexing

You can use negative indexing to access value of an array from the end. Follow the below examples to get the last element from the 1-dimensional array, and 2-dimensional array.


# Use Negative Array Index
arr = np.array([[0,3,5,7,9],[11,13,15,17,19]])
print('Last element from 1st dimensional: ', arr[0, -1])
print('Last element from 2nd dimensional: ', arr[1, -1])

# Output
#Last element from 1st dimensional:  9
#Last element from 2nd dimensional:  19

6. Conclusion

In this article, I have explained how to access values of the NumPy array in different ways by using array indexing with examples.

Happy Learning!!

You May Also Like

References

NumPy array indexing

Leave a Reply

You are currently viewing Python NumPy Array Indexing Examples