NumPy fill() Function with Examples

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:January 28, 2023

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 numpy.fill() function and using it syntax and paramerters how we can fill the Numpy array with values.

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.


# Below are the quick examples

# 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 with the for loop in python
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

We can use Numpy fill() function is used to fill the array with a specified value. This functions return the filled the array with the value 3 using the arr.fill() function.


import numpy as np

# Create array 
arr = np.arange(8)
  
# Use fill() function
arr.fill(3)
print(arr)

# Output:
# [3 3 3 3 3 3 3 3]

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, we’re indicating that we 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(arr2)

# Output:
# [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), we’re indicating that we 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(arr2)

# Output:
# [[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. We first created the NumPy array with the np.empty() function. It creates an array that contains only zero elements. We 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(arr)

# Output:
# [8 8 8 8 8 8]

We 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. We 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(arr)

# Output:
# [[2. 2. 2.]
#  [2. 2. 2.]
#  [2. 2. 2.]
#  [2. 2. 2.]]

6. Fill Array With Value With the for Loop in Python

We can also fill() the array using for loop for that, we need to create array, Here I will create NumPy array using np.empty() function. Then itrate using for loop it will return the array with specified value.


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

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

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

7. 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

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

You are currently viewing NumPy fill() Function with Examples