Python:NumPy take()
Anonymous contributor
Published Oct 31, 2025
Contribute to Docs
The take() method returns elements from an array at specified indices, similar to standard indexing but applicable across flattened or specified axes for more flexible element selection.
Syntax
ndarray.take(indices, axis=None, out=None, mode='raise')
Parameters:
indices: The 0-based indices of elements to extract.axis(Optional): Specifies the axis to take the elements. If not specified, the operation is done over a flattened array.out(Optional): An optional output array to store the selected elements. The shape and buffer length must match the expected output, but the type will be cast if necessary.mode(Optional): Specifies how out-of-bounds indices are handled:raise: Raises an error (default).wrap: Wrap around to the valid range.clip: Clips to the valid range.
Return value:
Returns an array containing the selected elements, or a reference to out if specified.
Example
This example demonstrates how to use the take() method to extract elements by index:
import numpy as nparr = np.array([10, 20, 30, 40, 50])result = arr.take([0, 3, 4])print(result)
The output of this code is:
[10 40 50]
Note: The
take()method can only extract elements along a single axis and cannot select multi-dimensional index combinations in one call. For example:
import numpy as nparr = np.array([[10, 20, 30],[40, 50, 60],[70, 80, 90]])# Invalid: cannot take (0,0) and (1,1) in one callarr.take([(0, 0), (1, 1)]) # Raises an error
The above will return an error.
Codebyte Example
In this example, ndarray.take() is applied to a 2D array to extract rows, columns, and wrapped indices:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours