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
>>> a = np.array([None, None], dtype=object) >>> a[0] = np.array(3) >>> a array([array(3), None], dtype=object) >>> a.fill(np.array(3)) >>> a array([array(3), array(3)], dtype=object)
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 create or fill an numpy array with another array? - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... How to create an numpy array with shape [2, 2, 3], where the elements at axis 2 is another array, for example [1, 2, 3]? More on stackoverflow.com
🌐 stackoverflow.com
How to fill a matrix using numpy
There's lots of ways to fill arrays. Here are some examples: import numpy as np arr = np.empty((3, 4)) # Value by value for i in range(arr.shape[0]): for j in range(arr.shape[1]): arr[i, j] = i + j # Row by row for i in range(arr.shape[0]): row = range(arr.shape[1]) arr[i, :] = row # Column by column for j in range(arr.shape[1]): row = range(arr.shape[0]) arr[:, j] = row # Or just all at once arr = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Or use one of many built-in functions arr = np.arange(3 * 4).reshape(3, 4) More on reddit.com
🌐 r/learnpython
3
2
June 18, 2020
Filling an array in python - Stack Overflow
I tried to create a new array and then fill it with for or while loop, but I can't get it fully filled. How can I do it in numpy with python without filling it manually? More on stackoverflow.com
🌐 stackoverflow.com
python - NumPy array initialization (fill with identical values) - Stack Overflow
I need to create a NumPy array of length n, each element of which is v. Is there anything better than: a = empty(n) for i in range(n): a[i] = v I know zeros and ones would work for v = 0, 1. I 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.])
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.6.dev0 Manual
>>> a = np.array([None, None], dtype=np.object_) >>> a[0] = np.array(3) >>> a array([array(3), None], dtype=object) >>> a.fill(np.array(3)) >>> a array([array(3), array(3)], dtype=object)
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.0 Manual
>>> a = np.array([None, None], dtype=object) >>> a[0] = np.array(3) >>> a array([array(3), None], dtype=object) >>> a.fill(np.array(3)) >>> a array([array(3), array(3)], dtype=object)
🌐
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 - The following code shows how to use the np.full() function to fill up a NumPy array of length 10 with the value 3 in each position:
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.fill.html
numpy.ndarray.fill — NumPy v2.2 Manual
>>> a = np.array([None, None], dtype=object) >>> a[0] = np.array(3) >>> a array([array(3), None], dtype=object) >>> a.fill(np.array(3)) >>> a array([array(3), array(3)], dtype=object)
🌐
Reddit
reddit.com › r/learnpython › how to fill a matrix using numpy
r/learnpython on Reddit: How to fill a matrix using numpy
June 18, 2020 -

Hello everyone,

I haven't done any type of linear algebra work with python before, only matlab. In matlab you create a matrix with the proper dimensions, then start filling it up with vectors of the appropriate size. I've tried to look up how to do this in python using a loop, and it doesn't seem as straightforward. It seems you first need to create a list, and append that list. Then convert that list to a numpy array. And then from there it appears you stack them? But stacking seems to only stack 1D and 2D arrays, so it's not exactly what I'm looking for.

For example:

import numpy as np
matrix=np.zeros((rows,columns))
list=[]
for i in range(columns):
    for x in range(rows):
        list.append(x)
    arr=np.array(list)
    matrix.append(arr) 
    list.clear()   

So in the above case, I've made a list, and converted it into an array with shape (rows, 1). Then I want to add it to the first column of my matrix. Then through the loop, I start to fill my matrix up by filling it one by one with these vectors. However, the issue is I'm trying to add a 1D array, to a multi-D matrix (which numpy doesn't allow you to do).

Hpw do I fill up a matrix using numpy?

🌐
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 - It returns a new array with the specified shape, filled entirely with the given value. ... import numpy as np array_shape = (3, 4) fill_value = 7 filled_array = np.full(array_shape, fill_value) print(filled_array)
🌐
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 › 2.1 › reference › generated › numpy.matrix.fill.html
numpy.matrix.fill — NumPy v2.1 Manual
>>> a = np.array([None, None], dtype=object) >>> a[0] = np.array(3) >>> a array([array(3), None], dtype=object) >>> a.fill(np.array(3)) >>> a array([array(3), array(3)], dtype=object)
🌐
Stack Overflow
stackoverflow.com › questions › 5891410 › numpy-array-initialization-fill-with-identical-values
python - NumPy array initialization (fill with identical values) - Stack Overflow
I need to create a NumPy array of length n, each element of which is v. Is there anything better than: a = empty(n) for i in range(n): a[i] = v I know zeros and ones would work for v = 0, 1. I
🌐
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.])
🌐
Medium
medium.com › @amit25173 › what-is-numpy-fill-and-when-to-use-it-6c6192c67b9d
What is numpy.fill and When to Use It? | by Amit Yadav | Medium
February 9, 2025 - Imagine having a blackboard, and ... what numpy.fill does for your NumPy arrays—it lets you reset every single value in an array to one consistent value, in just one line of code....
🌐
Towards Data Science
towardsdatascience.com › home › artificial intelligence › 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
August 31, 2022 - To create a NumPy array filled with ones, use the [np.ones()](https://numpy.org/doc/stable/reference/generated/numpy.ones.html) method as demonstrated below: The above demonstration creates a 2-dimensional NumPy array.
🌐
YouTube
youtube.com › watch
NumPy Filling arrays with Values In Python - YouTube
In this video we will learn how to Fill NumPy arrays with Values In Python. Please subscribe to support Asim Code!https://www.youtube.com/channel/UC2wyJKxwE...
Published: February 15, 2021