Tuesday, July 28, 2026
HomeLanguagesNumpy ndarray.itemset() function | Python

Numpy ndarray.itemset() function | Python

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]]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS