Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Numpy ndarray.itemset() function | Python

  • Last Updated : 22 Apr, 2020

numpy.ndarray.itemset() function insert scalar into an array.
There must be at least 1 argument, and define the last argument as item. Then, arr.itemset(*args) is equivalent to but faster than arr[args] = item. The item should be a scalar value and args must select a single item in the array arr.

Syntax : numpy.ndarray.itemset(*args)

Parameters :
*args : If one argument: a scalar, only used in case arr is of size 1. If two arguments: the last argument is the value to be set and must be a scalar, the first argument specifies a single array element location. It is either an int or a tuple.

Code #1 :




# Python program explaining
# numpy.ndarray.itemset() function
  
# importing numpy as geek 
import numpy as geek
  
geek.random.seed(345)
arr = geek.random.randint(9, size =(3, 3))
print("Input array : ", arr)
  
arr.itemset(4, 0)
  
print ("Output array : ", arr)

Output :

Input array :  [[8 0 3]
 [8 4 3]
 [4 1 7]]
Output array :  [[8 0 3]
 [8 0 3]
 [4 1 7]]

 
Code #2 :




# Python program explaining
# numpy.ndarray.itemset() function
  
# importing numpy as geek 
import numpy as geek
  
geek.random.seed(345)
arr = geek.random.randint(9, size =(3, 3))
print("Input array : ", arr)
  
arr.itemset((2, 2), 9)
  
print ("Output array : ", arr)

Output :

Input array :  [[8 0 3]
 [8 4 3]
 [4 1 7]]
Output array :  [[8 0 3]
 [8 4 3]
 [4 1 9]]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!