Python:NumPy .fill()

Anonymous contributor's avatar
Anonymous contributor
Published Jul 4, 2024Updated Oct 29, 2025
Contribute to Docs

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours