Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Working with Numpy Arrays: Indexing & Slicing

Oct 2, 2018 • 7 Minute Read

Introduction

Indexing and Slicing are two of the most common operations that you need to be familiar with when working with Numpy arrays. You will use them when you would like to work with a subset of the array. This guide will take you through a little tour of the world of Indexing and Slicing on multi-dimensional arrays.

Indexing

Indexing a One-dimensional Array

Let's talk about indexing a one-dimensional array. We have an array array1:

      import numpy as np
array1 = np.arange(0,10)
array1
    

Output:

      array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    

Similar to programming languages like Java and C#, the index starts with zero. So to access the third element in the array, use the index 2.

      array1[2]
    

Output:

      2
    

Indexing a Two-dimensional Array

Suppose I have a two-dimensional array:

      array1 = np.arange(12).reshape(4,3)
array1
    

Output:

      array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
    

To access elements in this array, use two indices. One for the row and the other for the column. Note that both the column and the row indices start with 0. So if I need to access the value ‘10,’ use the index ‘3’ for the row and index ‘1’ for the column.

      array1[3][1]
    

Output:

      10
    

Indexing a Three-dimensional Array

Let’s go one level higher. To access a three-dimensional array, include the index for the third dimension as well. It may be difficult to imagine a three-dimensional array, but let’s try our best.

First, let me create a three-dimensional array:

      array1 = np.arange(18).reshape(3,2,3)
array1
    

Output:

      array([[[ 0,  1,  2],
           [ 3,  4,  5]],

          [[ 6,  7,  8],
           [ 9, 10, 11]],

          [[12, 13, 14],
           [15, 16, 17]]])
    

Note that there are three two-dimensional arrays of size two by three. If you reshape the array into size (5,3,4), there will be five two-dimensional arrays with a size of three by four.

So, to retrieve the value ‘13’, first go the third two-dimensional array by specifying the index ‘2.’ And once you find the desired two-dimensional array, access the element you need.

For our case, you need to use the index [2], [0], and [1], where ‘0’ indicates the row 0 and ‘1’ indicates the column 1 within the third two-dimensional array.

      array1[2][0][1]
    

Output:

      13
    

And you can extend the same concept to higher dimensional arrays.

Slicing

Slicing a One-dimensional Array

To retrieve a single value, you use indexing. Similarly, to retrieve a collection of values, you would use slicing. For example, let me define a one-dimensional array

      array1 = np.arange(9)
array1
    

Output:

      array([0, 1, 2, 3, 4, 5, 6, 7, 8])
    

This is the syntax to slice the array:

      array1[3:6]
    

Output:

      array([3, 4, 5])
    

Index ‘3’ represents the starting element of the slice and it's inclusive. Index ‘6’ represents the stopping element of the slice and it’s exclusive. That's the reason why we did not get the value ‘6’ in the output.

If you do not specify the starting and the stopping index you will get all the values.

      array1[:]
    

Output:

      array([0, 1, 2, 3, 4, 5, 6, 7, 8])
    

That's because if the indices are missing, by default, Numpy inserts the starting and stopping indices that select the entire array. So writing array1[:] is equivalent to writing array1[0:9]

You can extend this concept to include only the starting index. In this case, the slice includes all the elements from the starting index until the end of the array. For example:

      array1[4:]
    

Output:

      array([4, 5, 6, 7, 8])
    

Or, alternatively, specify only the stopping index.

      array1[:7]
    

Output:

      array([0, 1, 2, 3, 4, 5, 6])
    

As expected, the slice includes all the elements from the start of the array until the indexed value. The rules for selecting the starting or the stopping element still hold true.

Using Negative Indices

You can also use negative values for more flexibility. To understand how negative values work, take a look at this picture below:

Each element of an array can be referenced with two indices. For example, both ‘3’ and ‘-6’ can be used to retrieve the value ‘40.’ First let’s declare an array with similar values:

      array1 = np.array([10,20,30,40,50,60,70,80,90])
array1
    

Output:

      array([10, 20, 30, 40, 50, 60, 70, 80, 90])
    

Using both ‘3’ and ‘-6’ gives the same value.

      array1[3]
    

Output:

      40
    
      array1[-6]
    

Output:

      40
    

You can use this trick to slice the array as well. For example,

      array1[3:-2]
    

Output:

      array([40, 50, 60, 70])
    

And here is a visual representation of how it works:

Let’s try once more. This time let’s use a negative value for both the indices.

      array1[-3:-1]
    

Output:

      array([70, 80])
    

But note that you cannot change the order of the indices. In other words, the slice operation cannot travel backwards. If you try to do that, you will get an empty array as the output. For example:

      array1[-1:-3]
    

Output:

      array([], dtype=int32)
    

Using Step Index

You can also include a step index if you would like to skip a few elements in your slice operation. For example:

      array1[2:6:3]
    

Output:

      array([30, 60])
    

‘2:6’ indicate the index positions for the slice operation. The value ‘3’ indicates the slice operation to step three elements after every selection.

Or, you can do something like this as well:

      array1[:7:2]
    

Output:

      array([10, 30, 50, 70])
    

Here, ‘:7’ means slice from ‘0:7’ and the last value ‘2’ indicates a step operation to step two elements after every selection.

Slicing a Two-dimensional Array

Let’s talk about slicing a two-dimensional array. I have this array array1

      array1 = np.arange(16).reshape(4,4)
array1
    

Output:

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

Slicing a two-dimensional array is very similar to slicing a one-dimensional array. You just use a comma to separate the row slice and the column slice. For example:

      array1[0:2, 1:3]
    

Output:

      array([[1, 2],
          [5, 6]])
    

Here is a pictorial representation:

Similarly, you can extend this for higher dimensional arrays.

Conclusion

Numpy Indexing and Slicing gives you powerful capabilities to select your data for further analysis. Understanding these basic operations will improve your skills in working with multidimensional arrays.

Ravikiran Srinivasulu

Ravikiran S.

Ravikiran is an independent cloud consultant and author focused on developing solutions in Microsoft Azure. His interests include everything in the cloud space, DevOps and Machine Learning with contributions in domains like Healthcare, Banking and Web Analytics. He is very passionate about the latest and futuristic technologies and constantly updates himself with the current technology trends. He works at the intersection of education and technology. In spare time, he likes going on long road trips with family and friends.

More about this author