Basic Slicing and Advanced Indexing in NumPy
Last Updated :
04 Nov, 2025
In this, we will cover basic slicing and advanced indexing in the NumPy. NumPy arrays are optimized for indexing and slicing operations making them a better choice for data analysis projects.
Indexing NumPy Array
Indexing is used to extract individual elements from a one-dimensional array. It can also be used to extract rows, columns or planes in a multi-dimensional NumPy array.
The table below shows positive and negative indexing positive indexing starts from 0 (left to right), while negative indexing starts from -1 (right to left).
Negative Index | -5 | -4 | -3 | -2 | -1 |
|---|
| Element | 23 | 21 | 55 | 65 | 23 |
|---|
| Positive Index | 0 | 1 | 2 | 3 | 4 |
|---|
Indexing Using Index arrays
Indexing with index arrays lets you fetch multiple elements from a NumPy array at once using their index positions. Unlike slicing, it returns a new copy of the data.
Example: Here, we create an array in decreasing order and use another array of indices to extract specific elements.
Python
import numpy as np
arr1 = np.arange(10, 1, -2)
print("Array:", arr1)
arr2 = arr1[[3, 1, 2]]
print("Elements at specified indices:", arr2)
Output('Array:', array([10, 8, 6, 4, 2]))
('Elements at specified indices:', array([4, 8, 6]))
Explanation: arr[[3, 1, 2]] creates a new array by picking elements from indices 3, 1 and 2 of the original array, resulting in [4, 8, 6].
Types of Indexing in NumPy Array
1. Basic Slicing and indexing
Basic slicing and indexing are used to access specific elements or a range of elements from a NumPy array. It returns a view of the original array (not a copy). The general syntax is x[start:stop:step], where:
- start: starting index (included)
- stop: ending index (excluded)
- step: interval between elements
Example: Here, we use different slicing patterns to access array elements.
Python
import numpy as np
a = np.arange(20)
print("a[15] =", a[15])
print("a[-8:17:1] =", a[-8:17:1])
print("a[10:] =", a[10:])
Output('a[15] =', 15)
('a[-8:17:1] =', array([12, 13, 14, 15, 16]))
('a[10:] =', array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]))
Using Ellipsis (…): Ellipsis (...) is used to represent all dimensions in a multi-dimensional array, making it easier to access deep elements.
Example: Here, we use ellipsis to access the second element across all inner lists.
Python
import numpy as np
b = np.array( [ [[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9],
[10, 11, 12]] ] )
print(b[..., 1])
Explanation: b[..., 1] accesses the second element (index 1) from each inner list across all dimensions.
2. Advanced indexing
Advanced indexing in NumPy allows you to extract complex data patterns using arrays of integers or Booleans. Unlike basic slicing, it returns a copy of the data, not a view. Advanced indexing occurs when the index object is:
- An ndarray of integers or Booleans
- A tuple with at least one sequence object
- A non-tuple sequence object
There are two types of Advanced Indexing in NumPy array indexing:
- Purely integer indexing
- Boolean integer indexing
Integer Array Indexing: This method selects elements using integer arrays. Each pair of indices from different dimensions identifies a specific element.
Example: Here, we extract elements using integer-based positions.
Python
import numpy as np
a = np.array( [[1, 2], [3, 4], [5, 6]] )
print( a[[0, 1, 2], [0, 0, 1]] )
Explanation: Here, positions (0,0), (1,0) and (2,1) are selected from the array, returning elements 1, 3 and 6.
Boolean Indexing: This indexing has some boolean expressions as the index. Those elements are returned which satisfy that Boolean expression. It is used for filtering the desired element values.
Example 1: Here, we select numbers greater than 50.
Python
import numpy as np
a = np.array([10, 40, 80, 50, 100])
print(a[a > 50])
Example 2: Here, we select rows whose sum is a multiple of 10.
Python
import numpy as np
b = np.array([[5, 5], [4, 5], [16, 4]])
res = b.sum(-1)
print(b[res % 10 == 0])
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice