NumPy fill() Function with Examples

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 10, 2023
  • Reading time:15 mins read

NumPy fill() function in Python is used to fill the array with a scalar value. This function fills the elements of an array with a static value from the specified start position to the end position. There are various ways to fill() the array with value in NumPy, for example by using NumPy full(), fill(), empty(), and for loop in Python.

In this article, I will explain the NumPy fill() function using its syntax and parameters, and usage how you can fill the Numpy array with values with examples.

1. Quick Examples of Fill Function

If you are in a hurry, below are some quick examples of how to use NumPy fill() function in Python.


# Quick examples of fill function

# Example 1: Use fill() function
arr = np.arange(8)
arr.fill(3)

# Example 2: Use numpy full() function 
# On 1-D array
arr2 = np.full(shape=7, fill_value=5)

# Example 3: Use numpy full() function 
# With two-dimensional arrays
arr2 = np.full(shape=(3, 2),fill_value=6)

# Example 4: Use numpy.fill() function 
# To fill array with value
arr = np.empty(6, dtype = int)
arr.fill(8)

# Example 5: Using fill() function 
# With multidimensional array
arr = np.empty([4, 3])
arr.fill(2)

# Example 6: Fill array with value 
# Use for loop 
arr = np.empty(8, dtype = int)
for i in range(8):
    arr[i] = 9

2. Syntax of NumPy fill()

Following is the syntax to create ndarray.fill() function.


# Syntax of ndarray.fill()
ndarray.fill(value)

2.1 Parameters of fill()

Following are the parameters of fill().

  • value – All elements of arr will be assigned this value.

3. Use numpy.fill() Function

You can use the Numpy fill() function to fill the array with a specified value. For instance, you create a NumPy array arr containing the integers from 0 to 7 using np.arange(8).

You use the fill() method to fill all elements of the arr array with the value 3. Finally, you print the array after filling it, which results in an array where all elements are 3.


# Import numpy
import numpy as np

# Create array 
arr = np.arange(8)
  
# Use fill() function
arr.fill(3)
print("After getting the fill array with a value:\n",arr)

Yields below output.

numpy fill

4. Use full() Function to Fill Array With Value

To create a one-dimensional Numpy array of size 7, with the value 5 use NumPy full() function. Here shape=7 is used to specify the length of the array, you’re indicating that you want the output to have seven elements and fill_value=5 specifies the array to be filled with value 7.


# Use numpy full() function on 1-D array
arr2 = np.full(shape=7, fill_value=5)
print("After getting the fill array with a value:\n",arr2)

# Output:
# After getting the fill array with a value:
#  [5 5 5 5 5 5 5]

Let’s create a two-dimensional Numpy array with 3 rows and 2 columns with the value 6 for all elements. By using shape=(3,2), you’re indicating that you want the output to have 3 rows and 2 columns. The code fill_value=6 fills that 3×2 array with 6.


# Use numpy full() function with two-dimensional arrays
arr2 = np.full(shape=(3, 2),fill_value=6)
print("After getting the fill array with a value:\n",arr2)

# Output:
# After getting the fill array with a value:
# [[6 6]
# [6 6]
# [6 6]]

5. Use NumPy fill() Function to Fill Array With Value

NumPy fill() function is used to fill an already existing NumPy array with similar values. This function takes the value and the datatype as input parameters and fills the array with the specified value. You first created the NumPy array with the np.empty() function. It creates an array that contains only zero elements. You then filled the array with the value 8 using the arr.fill(8) function.


# Create an empty NumPy array with length of 6
arr = np.empty(6, dtype = int)

# Use numpy.fill() function 
# To fill array with value
arr.fill(8)
print("After getting the fill array with a value:\n",arr)

# Output:
# After getting the fill array with a value:
#  [8 8 8 8 8 8]

You can also use numpy.ndarray.fill() function is used to work on the multidimensional arrays. This function fills up an empty NumPy array with the values 4 rows, and 3 columns in each position. You then filled the array with the value 2 using the arr.fill(2) function.


# Create an empty NumPy array with length of 4,3  
arr = np.empty([4, 3])
  
# Using fill() function with multidimensional array.
arr.fill(2)
print("After getting the fill array with a value:\n",arr)

# Output:
# After getting the fill array with a value:
# [[2. 2. 2.]
#  [2. 2. 2.]
#  [2. 2. 2.]
#  [2. 2. 2.]]

6. Fill Array With Value With the For Loop

You can also fill() the array using for loop for that, you need to create an array, Here I will create a NumPy array using np.empty() function. Then iterate using for loop it will return the array with a specified value.


# Fill array with value with the for loop
arr = np.empty(8, dtype = int)

# Use for loop 
for i in range(8):
    arr[i] = 9
print(arr)

# Output:
# [9 9 9 9 9 9 9 9]

Frequently Asked Questions

What is the purpose of the fill() function in NumPy?

The fill() function in NumPy serves the purpose of filling all elements of a NumPy array with a specified scalar value. It allows you to set all the elements of an array to a particular value, which can be useful in various numerical and scientific computing applications.

Is fill() a standalone function or a method of NumPy arrays?

fill() is a method of NumPy arrays, not a standalone function. In Python, methods are functions that are associated with object instances. In this case, fill() is associated with instances of NumPy arrays, allowing you to fill the elements of a specific array with a specified value. You call it on a NumPy array to modify the array in place by filling its elements with the specified value.

Can fill() be used with arrays of any shape and data type?

The fill() method in NumPy can be used with arrays of any shape and data type. You can apply the fill() method to NumPy arrays regardless of their dimensions (1D, 2D, 3D, etc.) and the data type of their elements (integer, float, etc.).

Does fill() modify the original array, or does it return a new array?

The fill() method modifies the original array in place. It does not return a new array. When you use the fill() method on a NumPy array, it changes the elements of that array to the specified value and does not create a new array.

Is there any alternative to fill() in NumPy?

If you want to create a new array filled with a specific value, you can use np.full() function.

Conclusion

In this article, you have learned how to numpy.fill() function using NumPy full(), fill(), for loop and empty() function with more examples.

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply