Python:NumPy .item()
Anonymous contributor
Published Oct 31, 2025
Contribute to Docs
The .item() method in Numpy copies an element of a NumPy array (ndarray) to a standard Python scalar (like int, float, bool) and returns it. The .item() method is handy for extracting a single value from an array, especially a 0-dimensional (scalar) array, into a native Python type.
Syntax
element = array.item(*args)
Parameters:
arrayis an instance ofndarray.*args:int, optional- If no arguments are provided, the array must have only one element (i.e.,
array.size == 1), and that single element is returned. - If a single integer is provided, it is interpreted as a flat index into the array (like indexing
array.ravel()[i]). - If a tuple of integers is provided, it is interpreted as a multi-dimensional index (e.g.,
arr.item((0, 1))for a 2D array).
- If no arguments are provided, the array must have only one element (i.e.,
Return Value:
Returns a standard Python scalar (e.g., int, float, bool) corresponding to the selected element.
Example: Retrieving elements using item()
The following example shows how to extract elements using item():
import numpy as np# 1D arrayarr_1d = np.array([10, 20, 30])element_1d = arr_1d.item(1) # Get element at index 1print(f"Element from 1D array: {element_1d}, Type: {type(element_1d)}")# 2D arrayarr_2d = np.array([[1, 2], [3, 4]])element_2d = arr_2d.item(0, 1) # Get element at row 0, column 1print(f"Element from 2D array: {element_2d}, Type: {type(element_2d)}")# 0D (scalar) arrayarr_0d = np.array(99)scalar_val = arr_0d.item() # No index needed for 0D arrayprint(f"Value from 0D array: {scalar_val}, Type: {type(scalar_val)}")
Output of the above example:
Element from 1D array: 20, Type: <class 'int'>Element from 2D array: 2, Type: <class 'int'>Value from 0D array: 99, Type: <class 'int'>
Codebyte Example
This example demonstrates the working of item(). It retrieves elements from different-shaped arrays using item():
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