🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.full()

AI & DATA SCIENCE // np-full

np.full() creates a new array of a given shape, filled entirely with a specified constant value.

Syntax

np.full(shape, fill_value, dtype=None)

Deep Dive Course

np.full(shape, value) directly allocates an array of the requested shape and fills every element with value, inferring the array's dtype from the fill value unless one is given explicitly. It's the most direct way to create an array pre-filled with any constant, more explicit than the common but slightly indirect pattern of multiplying an np.ones() array by that value, and unlike that pattern, it works cleanly even for values, like strings or specific dtypes, where multiplying wouldn't make sense.

1Understanding np.full()

np.full(shape, value) directly allocates an array of the requested shape and fills every element with value, inferring the array's dtype from the fill value unless one is given explicitly. It's the most direct way to create an array pre-filled with any constant, more explicit than the common but slightly indirect pattern of multiplying an np.ones() array by that value, and unlike that pattern, it works cleanly even for values, like strings or specific dtypes, where multiplying wouldn't make sense.

💡

Use np.full_like(other_array, value) instead of np.full(other_array.shape, value) when you want a filled array matching another array's shape and dtype exactly, without repeating that information manually.

editor.html
import numpy as np

arr = np.full((2, 3), 7)
print(arr)
localhost:3000

2Practical Example

Here is a real-world application of np.full() showing how it is used in production NumPy code.

editor.html
import numpy as np

template = np.zeros((2, 2))
filled = np.full_like(template, -1)
print(filled)
localhost:3000

3Best Practices

Follow these guidelines when working with np.full():

1. Use np.full(shape, value) directly instead of np.ones(shape) times value for filling an array with any constant other than 0 or 1

2. Specify dtype explicitly when the value's inferred type doesn't match what you actually need for the resulting array

3. Use np.full_like() when you want a filled array that matches an existing array's shape and dtype, rather than manually reading and passing its shape and dtype yourself

⚠️

Tip: Use np.full_like(other_array, value) instead of np.full(other_array.shape, value) when you want a filled array matching another array's shape and dtype exactly, without repeating that information manually.

editor.html
import numpy as np

arr = np.full((2, 3), 7)
print(arr)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.full((2, 3), 7)
print(arr)
Example 02Advanced Example
import numpy as np

template = np.zeros((2, 2))
filled = np.full_like(template, -1)
print(filled)

Best Practices

  • Use np.full(shape, value) directly instead of np.ones(shape) times value for filling an array with any constant other than 0 or 1
  • Specify dtype explicitly when the value's inferred type doesn't match what you actually need for the resulting array
  • Use np.full_like() when you want a filled array that matches an existing array's shape and dtype, rather than manually reading and passing its shape and dtype yourself

Interview Question

Why does np.full((3,), 'x') work fine, while multiplying np.ones((3,)) by 'x' would raise an error?

Hint: Think about what multiplication actually means for a string versus what filling an array means.

np.full() simply places the exact value you give it into every element of the array, regardless of what type that value is, including a string. Multiplying np.ones((3,)) by 'x', on the other hand, requires NumPy to perform actual multiplication between a numeric array and a string, which isn't a mathematically meaningful operation for NumPy's array arithmetic and raises a TypeError. This is one concrete reason np.full() is more broadly capable than the ones-times-value idiom, beyond just being more direct.

Exercises

MediumPractice using np.full() in a real scenario.
View Solution
import numpy as np

arr = np.full((2, 3), 7)
print(arr)

Frequently Asked Questions

Why does np.full((3,), 'x') work fine, while multiplying np.ones((3,)) by 'x' would raise an error?

np.full() simply places the exact value you give it into every element of the array, regardless of what type that value is, including a string. Multiplying np.ones((3,)) by 'x', on the other hand, requires NumPy to perform actual multiplication between a numeric array and a string, which isn't a mathematically meaningful operation for NumPy's array arithmetic and raises a TypeError. This is one concrete reason np.full() is more broadly capable than the ones-times-value idiom, beyond just being more direct.

Related Functions

np-zerosnp-onesnp-empty