Python | Numpy ndarray.item()

With the help of numpy.ndarray.item() method, we can fetch the data elements that is found at the given index on numpy array. Remember we can give index as one dimensional parameter or can be two dimensional.

Parameters:
*args : Arguments (variable number and type)
-> none: This argument only works when size of an array is 1.
-> int_type: This argument is interpreted as a flat index into the array, specifying which element to return.
-> tuple of int_types: This argument is interpreted as a two dimensional array, by specifying which element to return.

Returns: Copy of an Item

Example #1 :
In this example we can see that by specifying the argument in ndarray.item() method, we can have the element if it existed on this index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import the important module in python
import numpy as np
       
# make an array with numpy
gfg = np.array([1, 2, 3, 4, 5])
       
# applying ndarray.item() method
print(gfg.item(2))

chevron_right


Output:

3

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# import the important module in python
import numpy as np
       
# make an array with numpy
gfg = np.array([[1, 2, 3, 4, 5],
                [6, 5, 4, 3, 2]])
       
# applying ndarray.item() method
print(gfg.item((1, 2)))

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.