We use np.full() to quickly create arrays filled with the same value. This helps when you want a starting point or a fixed value repeated many times.
np.full() for custom-filled arrays in NumPy
import numpy as np array = np.full(shape, fill_value, dtype=None, order='C')
shape is the size of the array you want, like (3, 4) for 3 rows and 4 columns.
fill_value is the number you want to fill the array with.
import numpy as np # Create a 2x3 array filled with 7 array = np.full((2, 3), 7) print(array)
import numpy as np # Create an empty array with shape (0,) filled with 5 array = np.full((0,), 5) print(array)
import numpy as np # Create a 1-element array filled with 10 array = np.full((1,), 10) print(array)
import numpy as np # Create a 3x3 array filled with 0.5 and specify float type array = np.full((3, 3), 0.5, dtype=float) print(array)
This program creates a 4 by 5 array filled with 9. Then it changes one element to 0 to show how you can modify the array after creating it.
import numpy as np # Create a 4x5 array filled with 9 array_before = np.full((4, 5), 9) print("Array before change:") print(array_before) # Change the value at position (2,3) to 0 array_before[2, 3] = 0 print("\nArray after changing one element to 0:") print(array_before)
Time complexity is O(n) where n is the total number of elements, because it fills each element.
Space complexity is O(n) because it creates a new array of the given size.
A common mistake is to forget that shape must be a tuple, even for 1D arrays (use (5,) not 5).
Use np.full() when you want a fixed value repeated. Use np.zeros() or np.ones() if you only need zeros or ones.
np.full() creates arrays filled with the same value.
It needs the shape and the fill value as inputs.
Useful for initializing arrays with constant values for calculations or placeholders.
Practice
np.full() function do in NumPy?Solution
Step 1: Understand the purpose of np.full()
The function np.full() is designed to create arrays where every element has the same value.Step 2: Compare with other array creation functions
Unlike functions that create random or sequential arrays, np.full() fills the array with a constant value.Final Answer:
Creates an array filled with the same specified value -> Option DQuick Check:
np.full() = constant value array [OK]
- Confusing np.full() with np.random functions
- Thinking it creates empty or zero arrays
- Assuming it creates sequences of numbers
np.full()?Solution
Step 1: Identify the correct parameter order for np.full()
np.full() takes the shape as the first argument (tuple), then the fill value as the second argument.Step 2: Check each option's syntax
np.full((3, 3), 7) uses np.full((3, 3), 7) which matches the correct order and types. Others misuse argument order or syntax.Final Answer:
np.full((3, 3), 7) -> Option AQuick Check:
Shape tuple first, fill value second [OK]
- Passing shape and value as separate arguments without tuple
- Using keyword arguments incorrectly
- Swapping the order of shape and fill value
import numpy as np arr = np.full((2, 2), 5) print(arr)
Solution
Step 1: Understand np.full() parameters
The shape is (2, 2), so the array will have 2 rows and 2 columns. The fill value is 5.Step 2: Predict the printed array
The array will be a 2x2 matrix with all elements equal to 5, printed as [[5 5]\n [5 5]].Final Answer:
[[5 5] [5 5]] -> Option AQuick Check:
Shape (2,2) with 5 fills = [[5 5]\n [5 5]] [OK]
- Confusing shape dimensions
- Expecting zeros instead of fill value
- Misreading the printed array shape
import numpy as np arr = np.full(3, 4, 2) print(arr)
Solution
Step 1: Check np.full() argument count and order
np.full(shape, fill_value, dtype=None, ...). The call np.full(3, 4, 2) sets shape=3, fill_value=4, dtype=2, but 2 is not a valid dtype (raises ValueError: data type "2" not understood).Step 2: Confirm correct usage
Correct syntax is np.full(3, 4) for 1D array of 4s, or specify valid dtype like dtype=float. Extra or invalid dtype causes error.Final Answer:
The third positional argument `2` is an invalid dtype specifier -> Option CQuick Check:
np.full(3,4,2) -> ValueError: data type "2" [OK]
- Passing multiple positional arguments instead of tuple shape
- Using wrong data types for shape
- Assuming np.full() needs three arguments
np.full() and other NumPy functions?Solution
Step 1: Create a 4x4 array filled with 9
Using np.full((4,4), 9) creates a 4x4 array where every element is 9.Step 2: Replace diagonal elements with 0
np.fill_diagonal(arr, 0) sets the diagonal elements of the array to 0.Step 3: Verify the output
Printing arr shows a 4x4 array with 9 everywhere except 0 on the diagonal.Final Answer:
arr = np.full((4,4), 9); np.fill_diagonal(arr, 0); print(arr) -> Option BQuick Check:
Fill then fill_diagonal for custom values [OK]
- Filling zeros first then trying to fill diagonal with 9
- Using wrong shape or 1D array for 2D problem
- Misusing np.fill_diagonal on 1D arrays
Ready to prove it?
Pass the test and this pattern moves unknown
