Python:NumPy .fill()
Published Jul 4, 2024Updated Oct 29, 2025
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 NumPyimport numpy as a# Create a NumPy arraynf = a.arange(12)# Use the '.fill()' methodnf.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:
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