This comprehensive guide will teach you all the different ways to index and slice NumPy arrays. NumPy is an essential library for any data analyst or data scientist using Python. Effectively indexing and slicing NumPy arrays can make you a stronger programmer.

By the end of this tutorial, you’ll have learned:

- How NumPy array indexing and slicing works
- How to index one-dimensional, two-dimensional, and three-dimensional arrays
- How to slice NumPy arrays using ranges, conditions, and more

## Understanding NumPy Array Indexing

Much like working with Python lists, **NumPy arrays are based on a 0 index**. This means that the index starts at position 0 and continues through to the length of the list minus 1. Similarly, NumPy arrays can be negatively indexed, meaning that their last item can be accessed using the value of -1.

NumPy arrays go beyond basic Python lists by having a number of tricks up their sleeve. However, much of the functionality that exists for Python lists (such as indexing and slicing) will carry forward to NumPy arrays.

### How to Access One-Dimensional NumPy Array Elements with Indexing

Let’s see how we can access the first item in a NumPy array by using standard Python `x[obj]`

syntax, where `x`

is the array and `obj`

is the selection:

```
# Accessing the First Item in a NumPy Array
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[0])
# Returns: 1
```

In the code above, we were able to access the first item by indexing the 0^{th} index. Similarly, we can access the last item in a NumPy array by using the index of -1, as shown below:

```
# Accessing the Last Item in a NumPy Array
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[-1])
# Returns: 5
```

Now that you’ve learned how to index one-dimensional arrays, let’s take a look at how you can access items via indexing in two-dimensional arrays.

### How to Access Two-Dimensional NumPy Array Elements with Indexing

Accessing items in two dimensional NumPy arrays can be done in a number of helpful ways. Let’s first look at how to access items using traditional Python indexing by loading a two-dimensional array and double-indexing an array:

```
# Accessing the First Item in the Second Array
import numpy as np
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr[1][0])
# Returns: 4
```

We can see that by accessing the 0th index and then the 1st index, we can access the second item of the first array. This approach works in the same way as accessing items in nested lists of lists.

Let’s see a different, NumPy specific way of accessing items in a 2-dimensional NumPy array:

```
# Accessing the First Item in the Second Array (Alternate Method)
import numpy as np
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr[1, 0])
# Returns: 4
```

We can see that this simplifies the code significantly. By passing in a single index element containing two items, we’re able to drill into multiple dimensions. **This method is also more efficient – the method works without first creating a new, temporary array**.

What’s more, is that you can even combine this with negative indexing. This can be particularly helpful when you don’t know how many items an array has! Let’s see how we can access the last item of the first array:

```
# Using Negative Indexing with Two-Dimensions
import numpy as np
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr[0, -1])
# Returns: 3
```

This allows you to combine both positive and negative indices when indexing an array.

Now that you know how to work with two-dimensional arrays, let’s take a look at indexing three-dimensional arrays.

### How to Access Three-Dimensional NumPy Array Elements with Indexing

Accessing items in three-dimensional NumPy arrays works in much the same way as working with two-dimensional arrays. Since we know that accessing items works more efficiently by using a single square-bracket, let’s see how we can work three-dimensional arrays:

```
# Accessing Items in 3-D NumPy Arrays
import numpy as np
arr = np.array([
[
[1, 2],
[3, 4]
],
[
[5, 6],
[7, 8]
]
])
print(arr[-1, 0, 0])
# Returns: 5
```

In the example above, we create an array of shape `(2, 2, 2)`

. **The way that the indexing works is that it access data from the outside in**. In this case, we are accessing the last array, then the first array, and finally the first value – returning the value 5.

## Slicing and Striding NumPy Arrays

Similar to Python lists, you can slice and stride over NumPy arrays. This allows you to access multiple values in array from a starting position to a stop position, at a specific interval. Let’s take a look at a simpler example first, where we access items from the second to the second last item:

```
# Slicing NumPy Arrays
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:-1])
# Returns: [2 3 4]
```

By using slicing, the array **values from the start up to (but not including)** **the stop value are returned**.

The value before or after the colon `:`

is optional: if a number is omited, then the array is sliced from the first and to the last items respectively.

**We can also stride over the array at a particular interval using a third, optional argument in the slice**. This follows the convention of `[start : stop : stride]`

, where stride defaults to 1. Let’s see how we can stride over a NumPy array from the first to the last at an interval of 2:

```
# Striding Over a NumPy Array
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[::2])
# Returns: [1 3 5]
```

In the following section, you’ll learn how to use integer array indexing in NumPy to access multiple elements.

## Integer Array Indexing in NumPy to Access Multiple Elements

You can easily access multiple items via their index in a NumPy array by indexing using a list of items. This allows you to easily get multiple items without needing to index the array multiple times.

Let’s take a look at what this looks like by accessing the first and third item in an array:

```
# Indexing Multiple Items in a NumPy Array
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[[0, 2]])
# Returns: [1 3]
```

What’s important to note here is that by indexing you’re passing in a list of values (make note of the double square brackets).

This method also works with negative indices, as shown below:

```
# Indexing Multiple Items in a NumPy Array Using Negative Indices
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[[0, -1]])
# Returns: [1 5]
```

Similarly, the order of the indices doesn’t need to be sequential! You can access items in whatever order you want. In the example below, we access first the third item, then the first:

```
# Indexing Multiple Items in a NumPy Array Using Non-Sequential Indices
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[[2, 0]])
# Returns: [3 1]
```

In the section below, you’ll learn how to use boolean indexing in NumPy arrays for conditional slicing.

## Boolean Indexing in NumPy Arrays for Conditional Slicing

Using boolean indexing with NumPy arrays makes it very easy to index only items meeting a certain condition. This process is significantly simpler and more readable than normal ways of filtering lists. Let’s see how we can use boolean indexing to select only values under 3:

```
# Using Boolean Indexing in NumPy Arrays
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[arr < 3])
# Returns: [1 2]
```

Let’s break down what we’re doing in the code above:

- We create a boolean array by evaluating
`arr < 3`

which returns an array of boolean values of items meeting the condition - We index using this boolean array, returning only values less than 3

Similarly, we can use this method to filter in more complex ways. For example, if we only wanted to return even items, we could use the modulo operator to filter our array:

```
# Using Boolean Indexing for Even Values
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[arr % 2 == 0])
# Returns: [2 4]
```

## Conclusion

In this tutorial, you learned how to index NumPy arrays. You first learned simple indexing, allowing you to access a single value. Then, you learned how to work with two-dimensional and three-dimensional arrays. From there, you learned how to slice and stride over NumPy arrays, similar to working with Python lists. Finally, you learned powerful boolean indexing in order to index arrays based on a condition.

## Additional Resources

To learn more about related topics, check out the tutorials below: