tile and repeat are handy functions when you want to repeat an array in various ways:

In [233]: np.tile(np.array([4,6,6,1]),(3,1))
Out[233]: 
array([[4, 6, 6, 1],
       [4, 6, 6, 1],
       [4, 6, 6, 1]])

On the failure, note the docs for fill:

a.fill(value)

Fill the array with a scalar value.

np.array([4,6,6,1]) is not a scalar value. a was initialized as a 3 element float array.

It is possible to assign values to elements of an array, provided the shapes are right:

In [241]: a=np.empty(3)
In [242]: a[:]=np.array([1,2,3])    # 3 numbers into 3 slots
In [243]: a
Out[243]: array([ 1.,  2.,  3.])
In [244]: a=np.empty((3,4))
In [245]: a[:]=np.array([1,2,3,4])   # 4 numbers into 4 columns
In [246]: a
Out[246]: 
array([[ 1.,  2.,  3.,  4.],
       [ 1.,  2.,  3.,  4.],
       [ 1.,  2.,  3.,  4.]])

This fill works with an object type array, but the result is quite different, and should be used with considerable caution:

In [247]: a=np.empty(3, object)
In [248]: a
Out[248]: array([None, None, None], dtype=object)
In [249]: a.fill(np.array([1,2,3,4]))
In [250]: a
Out[250]: array([array([1, 2, 3, 4]), array([1, 2, 3, 4]), array([1, 2, 3, 4])], dtype=object)

This (3,) array is not the same as the (3,4) array produced by other methods. Each element of the object array is a pointer to the same thing. Changing a value in one element of a changes that value in all the elements (because they are the same object).

In [251]: a[0][3]=5
In [252]: a
Out[252]: array([array([1, 2, 3, 5]), array([1, 2, 3, 5]), array([1, 2, 3, 5])], dtype=object)
Answer from hpaulj on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.1 Manual
>>> import numpy as np >>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.]) Fill expects a scalar value and always behaves the same as assigning to a single array element.
Top answer
1 of 6
12

tile and repeat are handy functions when you want to repeat an array in various ways:

In [233]: np.tile(np.array([4,6,6,1]),(3,1))
Out[233]: 
array([[4, 6, 6, 1],
       [4, 6, 6, 1],
       [4, 6, 6, 1]])

On the failure, note the docs for fill:

a.fill(value)

Fill the array with a scalar value.

np.array([4,6,6,1]) is not a scalar value. a was initialized as a 3 element float array.

It is possible to assign values to elements of an array, provided the shapes are right:

In [241]: a=np.empty(3)
In [242]: a[:]=np.array([1,2,3])    # 3 numbers into 3 slots
In [243]: a
Out[243]: array([ 1.,  2.,  3.])
In [244]: a=np.empty((3,4))
In [245]: a[:]=np.array([1,2,3,4])   # 4 numbers into 4 columns
In [246]: a
Out[246]: 
array([[ 1.,  2.,  3.,  4.],
       [ 1.,  2.,  3.,  4.],
       [ 1.,  2.,  3.,  4.]])

This fill works with an object type array, but the result is quite different, and should be used with considerable caution:

In [247]: a=np.empty(3, object)
In [248]: a
Out[248]: array([None, None, None], dtype=object)
In [249]: a.fill(np.array([1,2,3,4]))
In [250]: a
Out[250]: array([array([1, 2, 3, 4]), array([1, 2, 3, 4]), array([1, 2, 3, 4])], dtype=object)

This (3,) array is not the same as the (3,4) array produced by other methods. Each element of the object array is a pointer to the same thing. Changing a value in one element of a changes that value in all the elements (because they are the same object).

In [251]: a[0][3]=5
In [252]: a
Out[252]: array([array([1, 2, 3, 5]), array([1, 2, 3, 5]), array([1, 2, 3, 5])], dtype=object)
2 of 6
8

Use broadcasting

vstack, tile, and repeat are all great and whatnot, but broadcasting can be several orders of magnitude faster...

import numpy as np
from time import time

t = time()
for _ in xrange(10000):
    a = np.array([4,6,6,1])
    b = np.vstack((a,)*100)
print time()-t

t = time()
for _ in xrange(10000):
    a = np.array([4,6,6,1])
    b = np.tile(a,(3,1))
print time()-t

t = time()
for _ in xrange(10000):
    a = np.array([4,6,6,1])
    b = np.empty([100,a.shape[0]])
    b[:] = a
print time()-t

prints:

2.76399993896
0.140000104904
0.0490000247955
Discussions

python - How to declare and fill an array in NumPy? - Stack Overflow
I need to create an empty array in Python and fill it in a loop method. More on stackoverflow.com
🌐 stackoverflow.com
Trying to fill an empty numpy array with data
That looks like a csv file. Just use numpy's genfromtext function to read it. arr = np.genfromtxt(filename, dtype=int, delimiter=',') More on reddit.com
🌐 r/learnpython
26
2
December 7, 2016
python - Best way to initialize and fill an numpy array? - Stack Overflow
Definition: np.full(shape, fill_value, dtype=None, order='C') Docstring: Return a new array of given shape and type, filled with `fill_value`. Although I think this might be only available in numpy 1.8+ More on stackoverflow.com
🌐 stackoverflow.com
python - Numpy array, fill empty values for a single column - Stack Overflow
I have an array in numpy, which was generated using np.array() from a python list so my entries are strings, but some of the values are blank. Here is an example array: Copy['1', '1', '1', '1'] ['1', '1', '', '1'] ['1', '1', '1', '1'] ['1', '', '1', '1'] There is no 'NaN' or 'None', it is blank. I want to be able to fill ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.5 Manual
>>> import numpy as np >>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.]) Fill expects a scalar value and always behaves the same as assigning to a single array element.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.6.dev0 Manual
>>> import numpy as np >>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.]) Fill expects a scalar value and always behaves the same as assigning to a single array element.
🌐
Statology
statology.org › home › how to fill numpy array with values (2 examples)
How to Fill NumPy Array with Values (2 Examples)
July 21, 2022 - You can use the following methods to fill a NumPy array with values: ... #create empty NumPy array with length of 10 my_array = np.empty(10) #fill NumPy array with 3's my_array.fill(3)
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.2 Manual
>>> import numpy as np >>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.]) Fill expects a scalar value and always behaves the same as assigning to a single array element.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.0 Manual
>>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.]) Fill expects a scalar value and always behaves the same as assigning to a single array element.
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.3 Manual
>>> import numpy as np >>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.]) Fill expects a scalar value and always behaves the same as assigning to a single array element.
🌐
SciPy
docs.scipy.org › doc › numpy-1.15.0 › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v1.15 Manual
July 24, 2018 - numpy.ndarray · index · next · previous · ndarray.fill(value)¶ · Fill the array with a scalar value. Examples · >>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([ 1., 1.]) numpy.ndarray.dumps · numpy.ndarray.flatten ·
🌐
NumPy
numpy.org › doc › 1.16 › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v1.16 Manual
>>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([ 1., 1.]) numpy.ndarray.dumps · numpy.ndarray.flatten · © Copyright 2008-2019, The SciPy community. Last updated on Feb 18, 2020.
🌐
Delft Stack
delftstack.com › home › howto › numpy › python fill array with value
How to Fill Array With Value in NumPy | Delft Stack
March 11, 2025 - In this example, we first create an empty array with the desired shape using np.empty(). Then, we use nested for loops to iterate through each element of the array and assign the fill_value of 5. This method is more flexible, allowing you to implement complex filling logic if needed, but it’s generally less efficient than the previous methods, especially for large arrays. Filling a NumPy array with a specific value is a fundamental operation that can be accomplished in several ways.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › how-to-create-an-empty-and-a-full-numpy-array
How to create an empty and a full NumPy array - GeeksforGeeks
September 19, 2025 - numpy.full(shape, fill_value, dtype=None, order='C') ... Example: This code creates a 3x4 array filled with the value 5 using np.full(). ... Unlike np.empty(), this array is fully initialized with the given value.
🌐
AskPython
askpython.com › python-modules › numpy › numpy-empty-and-empty_like
NumPy empty and empty_like - AskPython
October 4, 2026 - When you’re about to fill a NumPy array with your own values straight away, you only need NumPy to set aside the space. np.empty creates that array without setting its values, and np.empty_like does the same using another array’s shape and ...
🌐
Finxter
blog.finxter.com › home › learn python blog › how to create a numpy array and fill it with nan values?
How to Create a NumPy Array and Fill It With NaN Values?  - Be on the Right Side of Change
October 15, 2022 - Now that we’ve seen the different methods available to create an empty NaN array, let’s determine the execution time in nanoseconds for all of the above methods. To create a NaN array of size 3*3 let’s check how much time each method is going to take. Let’s use the perf_counter_ns from the time module to determine the execution time. import numpy as np from time import perf_counter_ns #Using np.fill() start=perf_counter_ns() arr=np.empty((3,3)) #create an empty array arr.fill(np.NaN) #Fill the array with Nan values end=perf_counter_ns() print("Execution Time using fill : ]ns"%(end-star
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matrix.fill.html
numpy.matrix.fill — NumPy v2.1 Manual
>>> import numpy as np >>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.]) Fill expects a scalar value and always behaves the same as assigning to a single array element.