NumPy 1.8 introduced np.full(), which is a more direct method than empty() followed by fill() for creating an array filled with a certain value:

>>> np.full((3, 5), 7)
array([[ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.]])

>>> np.full((3, 5), 7, dtype=int)
array([[7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7]])

This is arguably the way of creating an array filled with certain values, because it explicitly describes what is being achieved (and it can in principle be very efficient since it performs a very specific task).

Answer from Eric O. Lebigot on Stack Overflow
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Create an array with the same value (np.zeros, np.ones, np.full) | note.nkmk.me
January 23, 2024 - To fill an existing array with any values, select all elements with a slice and assign the new value. ... You can omit the trailing : and use just [:] to select all elements regardless of the number of dimensions.
🌐
Medium
medium.com › @amit25173 › trick-to-quickly-numpy-create-array-with-same-value-5fac963d3921
Trick to Quickly numpy create array with same value | by Amit Yadav | Medium
April 12, 2025 - The easiest way to create an array filled with the same value is by using NumPy’s numpy.full() function. This function allows you to specify the size of the array and the value you want to fill it with.
🌐
CodeSpeedy
codespeedy.com › home › how to create or initialize an array with same values in python
How to create or initialize an array with same values in Python
March 17, 2022 - # Importing numpy module import numpy as np np.full(10, 7) #This will create array of number 7 repeated 10 times ... As you can see using the full() function array of similar elements is created. The full() function takes a parameter size and ...
🌐
IncludeHelp
includehelp.com › python › numpy-array-initialization-fill-with-identical-values.aspx
Python - NumPy array initialization (fill with identical values)
May 23, 2023 - To initialize a NumPy array and fill with identical values, you can use a method provided by NumPy called the full() method. This method is better than the empty() followed by the fill() method. This is arguably the way of creating an array filled with certain values because it explicitly describes ...
🌐
O'Reilly
oreilly.com › library › view › scipy-recipes › 9781788291460 › bcf20cf0-31d2-41d8-83db-d0a5a25f50de.xhtml
Creating an array with the same shape as another array - SciPy Recipes [Book]
December 20, 2017 - Creating an array with the same shape as another array NumPy provides a family of functions that create an array with the same shape as another input array. These functions... - Selection from SciPy Recipes [Book]
Authors: Luiz Felipe MartinsKe WuRuben Oliva RamosV Kishore Ayyadevara
Published: 2017
Pages: 386
🌐
w3resource
w3resource.com › python-exercises › numpy › basic › numpy-basic-exercise-55.php
NumPy: Create an array of equal shape and data type of a given array with fixed value - w3resource
August 28, 2025 - Write a NumPy program to generate ... function. Create a new array with the same structure as an input array, then fill it with alternating values based on index parity while preserving the original dtype....
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › user › basics.creation.html
Array creation — NumPy v2.5 Manual
In the third example, the array is dtype=np.float64 to accommodate the step size of 0.1. Due to roundoff error, the stop value is sometimes included. numpy.linspace will create arrays with a specified number of elements, and spaced equally between the specified beginning and end values.
Top answer
1 of 4
14

There are lots of ways to do this. The first one-liner that occurred to me is tile:

>>> numpy.tile(2, 25)
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
       2, 2, 2, 2, 2])

You can tile a value in any shape:

>>> numpy.tile(2, (5, 5))
array([[2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2]])

However, as a number of answers below indicate, this isn't the fastest method. It's designed for tiling arrays of any size, not just single values, so if you really just want to fill an array with a single value, then it's much faster to allocate the array first, and then use slice assignment:

>>> a = numpy.empty((5, 5), dtype=int)
>>> a[:] = 2
>>> a
array([[2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2]])

According to a few tests I did, there aren't any faster approaches. However, two of the approaches mentioned in answers below are equally fast: ndarray.fill and numpy.full.

These tests were all done in ipython, using Python 3.6.1 on a newish mac running OS 10.12.6. Definitions:

def fill_tile(value, shape):
    return numpy.tile(value, shape)

def fill_assign(value, shape, dtype):
    new = numpy.empty(shape, dtype=dtype)
    new[:] = value
    return new

def fill_fill(value, shape, dtype):
    new = numpy.empty(shape, dtype=dtype)
    new.fill(value)
    return new

def fill_full(value, shape, dtype):
    return numpy.full(shape, value, dtype=dtype)

def fill_plus(value, shape, dtype):
    new = numpy.zeros(shape, dtype=dtype)
    new += value
    return new

def fill_plus_oneline(value, shape, dtype):
    return numpy.zeros(shape, dtype=dtype) + value

for f in [fill_assign, fill_fill, fill_full, fill_plus, fill_plus_oneline]:
    assert (fill_tile(2, (500, 500)) == f(2, (500, 500), int)).all()

tile is indeed quite slow:

In [3]: %timeit fill_tile(2, (500, 500))
947 µs ± 10.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Slice assignment ties with ndarray.fill and numpy.full for first place:

In [4]: %timeit fill_assign(2, (500, 500), int)
102 µs ± 1.37 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [5]: %timeit fill_fill(2, (500, 500), int)
102 µs ± 1.99 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [6]: %timeit fill_full(2, (500, 500), int)
102 µs ± 1.47 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In-place broadcasted addition is only slightly slower:

In [7]: %timeit fill_plus(2, (500, 500), int)
179 µs ± 3.7 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

And non-in-place broadcasted addition is only slightly slower than that:

In [8]: %timeit fill_plus_oneline(2, (500, 500), int)
213 µs ± 4.74 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2 of 4
6

How about:

shape = (100,100)
val = 3.14
dt = np.float
a = np.empty(shape,dtype=dt)
a.fill(val)

This way you can set things and pass the parameters in. Also, in terms of timings

In [35]: %timeit a=np.empty(shape,dtype=dt); a.fill(val)
100000 loops, best of 3: 13 us per loop

In [36]: %timeit a=np.tile(val,shape)
10000 loops, best of 3: 102 us per loop

So using empty with fill seems significantly faster than tile.

🌐
Towards Data Science
towardsdatascience.com › home › latest › here are 30 ways that will make you a pro at creating numpy arrays
Here Are 30 Ways That Will Make You a Pro at Creating NumPy Arrays | Towards Data Science
January 18, 2025 - The first argument is the shape of the array ((2, 4) in this case), and the second argument is the fill value (5). Lastly, if you want to create a NumPy array of a given shape and type without initializing entries, use the [np.empty()](https://numpy.org/doc/stable/reference/generated/numpy.empty.html) method:
🌐
Jmgphd
jmgphd.com › courses › csc5930 › lecture-notes › numpy-arrays
NumPy Arrays – Jason M. Grant
Let’s look at four (4) approaches for creating arrays filled with the same value. The first is np.zeros. As you probably guessed, this fills an entire array with zeros. The function needs to know the size of the array, which can be an integer for a one-dimensional array or a tuple when creating a multi-dimensional array. Similarly, NumPy’s np.ones creates an array filled with ones.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.full.html
numpy.full — NumPy v2.2 Manual
Return a new array with shape of input filled with value. ... Return a new uninitialized array. ... Return a new array setting values to one. ... Return a new array setting values to zero. ... >>> import numpy as np >>> np.full((2, 2), np.inf) array([[inf, inf], [inf, inf]]) >>> np.full((2, 2), 10) array([[10, 10], [10, 10]])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty_like.html
numpy.empty_like — NumPy v2.5 Manual
Unlike other array creation functions (e.g. zeros_like, ones_like, full_like), empty_like does not initialize the values of the array, and may therefore be marginally faster. However, the values stored in the newly allocated array are arbitrary. For reproducible behavior, be sure to set each element of the array before reading. ... Try it in your browser! >>> import numpy as np >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[-1073741821, -1073741821, 3], # uninitialized [ 0, 0, -1073741821]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000], # uninitialized [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.full.html
numpy.full — NumPy v2.5 Manual
Return a new array with shape of input filled with value. ... Return a new uninitialized array. ... Return a new array setting values to one. ... Return a new array setting values to zero. ... Try it in your browser! >>> import numpy as np >>> np.full((2, 2), np.inf) array([[inf, inf], [inf, inf]]) >>> np.full((2, 2), 10) array([[10, 10], [10, 10]])
🌐
pythoncodelab
pythoncodelab.com › home › python create a list of size n with the same value
Python Create a List of Size N with the Same Value -
November 2, 2024 - import numpy as np dimensions=(2,4) one_array=np.ones(dimensions) one_list=one_array.tolist() print('Python list having four one',one_list) output · Python list having four one [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]] The above code creates a array of (2 x 4) containing value 1 which is converted into list using tolist() function.
🌐
NumPy
numpy.org › doc › 2.4 › reference › routines.array-creation.html
Array creation routines — NumPy v2.4 Manual
empty(shape[, dtype, order, device, like]) · Return a new array of given shape and type, without initializing entries