All IT Courses 50% Off
Artificial Intelligence Tutorials

Accessing elements of a Numpy Array through Slicing and Indexing

Just like in a list, element(s) of an array can be accessed through slicing and indexing. And it’s virtually the same process as in a list. In this tutorial, we will discuss how to index and array and go further to discuss array slicing. Let’s begin with array indexing. 

Accessing a Single Element in an Array through Slicing 

To slice an array, the index of the value to be accessed is specified in square brackets. Remember that when counting the index from the front, the counting begins from zero in Python. Thus, the first element has an index of 0, the second element has an index 1, and so on. See the example below. 

#import the necessary library
import numpy as np
 
array1 = np.array([2, 4, 6, 8, 10])
 
print(array1[0])
Output:
2

The first element is printed. 

To print the 5th element, index 4 is specified in square brackets. 

print(array1[4])
Output:
10

Counting the Array from the last Element

You can also count from behind. To index from the last element of the array, negative indices are used. The last element has an index of -1, the second to the last has an index of -2, and so on. See the example below. 

All IT Courses 50% Off
#import the necessary library
import numpy as np
 
array1 = np.array([2, 4, 6, 8, 10])
 
#accessing the last element in the list
print(array1[-1])
Output:
10

The last element is expected. 

print(array1[-2])
Output:
8

You can also access an element in a multidimensional array. The indices of the element to be accessed are separated by a comma and specified in square brackets. 

#import the necessary library
import numpy as np
 
array1 = np.array([[12, 3, 9], 
                    [25, 0, 13],
                    [5, 8, 10]])
 
print(array1[1, 2])

Output:
13

In the above example, we accessed the element in the 2nd row and 3rd column. 

Accessing Sub Arrays from a Numpy Array through Slicing 

You can access subarrays in an array using the slicing notation (:). As with slicing a list in Python, the slice of a Numpy array is done using the syntax below. 

array[start:stop:step]

By default, the step=1, the start=0, and the stop= size of the array. So if you wish to return the same array, you can write array[::] or better still array[:]. 

Let’s see some coding examples. 

Defining only the start index

When only the start index is defined, the program returns the elements from the start index to the end of the array

#import the necessary library
import numpy as np
 
array1 = np.array([2, 4, 6, 8, 10])
 
print(array1[1:])
Output:
[ 4  6  8 10]

Define the start and stop index

When the stop index is specified, the program does stop at the element just before the stop index. It does not return the end index.  

#import the necessary library
import numpy as np
 
array1 = np.array([2, 4, 6, 8, 10])
 
print(array1[1:3])
Output:
[4 6]

Notice that the element in index 3, (8) was not included in the output. This is critical to understand. The Python interpreter does not include the stop index during slicing. 

You can also specify the step. 

#import the necessary library
import numpy as np
 
array1 = np.array([2, 4, 6, 8, 10])
 
print(array1[::2])
Output:
[ 2  6 10]

As seen, the program prints elements after every 2 steps. 

Slicing Multidimensional Arrays 

You can access the subarrays from an array through slicing as well.

#import the necessary library
import numpy as np
 
array1 = np.array([[2, 4, 6, 8, 10],
                    [12, 54, 24, 77, 34],
                    [1, 32, 78, -2, 87],
                    [0, 15, 29, 71, 88]])
 
print(array1[1:3, :3])

In the code above, the intersection of the second to the third row and the first 3 columns will be accessed.

Output:
[[12 54 24]
 [ 1 32 78]]

As expected. 

You can play around with the index to get different results. You can refer to these articles to learn more about Python lists and Python arrays. If you have any questions, feel free to leave them in the comment section and I’d do my best to answer them. 

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button