numpy.ndarray.fill() in Python Last Updated : 28 Dec, 2018 Comments Improve Suggest changes Like Article Like Report numpy.ndarray.fill() method is used to fill the numpy array with a scalar value. If we have to initialize a numpy array with an identical value then we use numpy.ndarray.fill(). Suppose we have to create a NumPy array a of length n, each element of which is v. Then we use this function as a.fill(v). We need not use loops to initialize an array if we are using this fill() function. Syntax : ndarray.fill(value) Parameters: value : All elements of a will be assigned this value. Code #1: Python3 1== # Python program explaining # numpy.ndarray.fill() function import numpy as geek a = geek.empty([3, 3]) # Initializing each element of the array # with 1 by using nested loops for i in range(3): for j in range(3): a[i][j] = 1 print("a is : \n", a) # now we are initializing each element # of the array with 1 using fill() function. a.fill(1) print("\nAfter using fill() a is : \n", a) Output: a is : [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]] After using fill() a is : [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]] Code #2: Python3 # Python program explaining # numpy.ndarray.fill() function import numpy as geek a = geek.arange(5) print("a is \n", a) # Using fill() method a.fill(0) print("\nNow a is :\n", a) Output: a is [0 1 2 3 4] Now a is : [0 0 0 0 0] Code #3: numpy.ndarray.fill() also works on multidimensional array. Python3 # Python program explaining # numpy.ndarray.fill() function import numpy as geek a = geek.empty([3, 3]) # Using fill() method a.fill(0) print("a is :\n", a) Output: a is : [[ 0. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] Comment More infoAdvertise with us Next Article numpy.ndarray.fill() in Python A ArkadipGhosh Follow Improve Article Tags : Python Python-numpy Python numpy-ndarray Practice Tags : python Similar Reads numpy.ndarray.flat() in Python The numpy.ndarray.flat() function is used as a 1_D iterator over N-dimensional arrays. It is not a subclass of, Pythonâs built-in iterator object, otherwise it a numpy.flatiter instance. Syntax : numpy.ndarray.flat() Parameters : index : [tuple(int)] index of the values to iterate Return :  1-D i 3 min read 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 2 min read numpy.ndarray.view() in Python numpy.ndarray.view() helps to get a new view of array with the same data. Syntax: ndarray.view(dtype=None, type=None)Parameters: dtype : Data-type descriptor of the returned view, e.g., float32 or int16. The default, None, results in the view having the same data-type as a. type : Python type, opti 3 min read numpy.full() in Python numpy.full(shape, fill_value, dtype = None, order = 'C') : Return a new array with the same shape and type as a given array filled with a fill_value. Parameters : shape : Number of rows order : C_contiguous or F_contiguous dtype : [optional, float(by Default)] Data type of returned array. fill_value 1 min read Python | Numpy ndarray.__copy__() With the help of Numpy ndarray.__copy__() method, we can make a copy of all the data elements that is present in numpy array. If you change any data element in the copy, it will not affect the original numpy array. Syntax : numpy.__copy__() Return : Copy of all the data elements Example #1 : In this 1 min read Python | Numpy MaskedArray.__iand__() numpy.ma.MaskedArray class is a subclass of ndarray designed to manipulate numerical arrays with missing data. With the help of Numpy MaskedArray.__iand__we can get the elements that is anded by the value that is provided as a parameter in the MaskedArray.__iand__() method. Syntax: numpy.MaskedArray 1 min read Python | Numpy MaskedArray.__ilshift__() numpy.ma.MaskedArray class is a subclass of ndarray designed to manipulate numerical arrays with missing data. With the help of Numpy MaskedArray.__ilshift__we can get the elements that is left shifted by the value that is provided as a parameter in the MaskedArray.__ilshift__() method. Syntax: nump 1 min read Numpy ndarray.flatten() function in Python The flatten() function is used to convert a multi-dimensional NumPy array into a one-dimensional array. It creates a new copy of the data so that original array stays unchanged. If your array has rows and columns or even more dimensions, then flatten() line up every single value into a straight list 3 min read numpy.ma.filled() function - Python numpy.ma.filled() function return input as an array with masked data replaced by a fill value. If arr is not a MaskedArray, arr itself is returned. If arr is a MaskedArray and fill_value is None, fill_value is set to arr.fill_value. Syntax : numpy.ma.filled(arr, fill_value = None) Parameters : arr : 1 min read Python | Numpy MaskedArray.__ior__() numpy.ma.MaskedArray class is a subclass of ndarray designed to manipulate numerical arrays with missing data. With the help of Numpy MaskedArray.__ior__we can get the elements that is OR by the value that is provided as a parameter in the MaskedArray.__ior__() method. Syntax: numpy.MaskedArray.__io 1 min read Like