Learn | NEPs | User Guide | API reference | Building from source | Development | Release notes | NumPy’s module structure | Array objects | The N-dimensional array (ndarray) | Scalars | Data type objects (dtype) | Data type promotion in NumPy | Iterating over arrays | Standard array subclasses | Masked arrays | The array interface protocol | Datetimes and timedeltas | Universal functions (ufunc) | Routines and objects by topic | Typing (numpy.typing) | NumPy C-API | Array API standard compatibility | CPU/SIMD optimizations | Thread Safety | Global Configuration Options | NumPy security | Testing guidelines
method
ndarray.fill(value)
Fill the array with a scalar value.
Parameters:valuescalarAll elements of a will be assigned this value.
Examples
>>> importnumpyasnp>>> a=np.array([1,2])>>> a.fill(0)>>> aarray([0, 0])>>> a=np.empty(2)>>> a.fill(1)>>> aarray([1., 1.])Fill expects a scalar value and always behaves the same as assigning to a single array element. The following is a rare example where this distinction is important:
>>> a=np.array([None,None],dtype=np.object_)>>> a[0]=np.array(3)>>> aarray([array(3), None], dtype=object)>>> a.fill(np.array(3))>>> aarray([array(3), array(3)], dtype=object)Where other forms of assignments will unpack the array being assigned:
>>> a[...]=np.array(3)>>> aarray([3, 3], dtype=object)