.fiil()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Jul 4, 2024
Contribute to Docs

The .fill() method is used to fill a NumPy array with a specified scalar value.

Syntax

ndarray.fill(value)
  • ndarray: The NumPy array to be filled.
  • value: The scalar value to assign to all the elements in the array.

Example

In this example, the .fill() method assigns the scalar value 2798 to all the elements in the one-dimensional NumPy array nf:

# Import NumPy
import numpy as a
# Create a NumPy array
nf = a.arange(12)
# Use the '.fill()' method
nf.fill(2798)
print(nf)

We will get the following result:

[2798 2798 2798 2798 2798 2798 2798 2798 2798 2798 2798 2798]

Codebyte Example

In the following codebyte example, a two-dimensional NumPy array nf is created with specific values and then filled entirely with 985:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python:NumPy on Codecademy