[ Web Proxy ]
URL:
Viewing: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.fill.html#numpy.ndarray.fill [Back]  [Original]

numpy.ndarray.fill — NumPy v2.5 Manual
Back to top
Search the docs ...:

numpy.ndarray.fill#

method

ndarray.fill(value)#

Fill the array with a scalar value.

Parameters:
valuescalar

All elements of a will be assigned this value.

Examples

Try it in your browser!
>>> import numpy as np
>>> a = np.array([1, 2])
>>> a.fill(0)
>>> a
array([0, 0])
>>> a = np.empty(2)
>>> a.fill(1)
>>> a
array([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)
>>> a
array([array(3), None], dtype=object)
>>> a.fill(np.array(3))
>>> a
array([array(3), array(3)], dtype=object)

Where other forms of assignments will unpack the array being assigned:

>>> a[...] = np.array(3)
>>> a
array([3, 3], dtype=object)

Web Proxy Viewer  |  New URL  |  Original Page