# NumPy array initialization (fill with identical values) – Dev

The best answers to the question “NumPy array initialization (fill with identical values)” in the category Dev.

QUESTION:

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 could use `v * ones(n)`, but it won’t work when `v` is `None`, and also would be much slower.

Updated for Numpy 1.7.0:(Hat-tip to @Rolf Bartstra.)

`a=np.empty(n); a.fill(5)` is fastest.

In descending speed order:

``````%timeit a=np.empty(10000); a.fill(5)
100000 loops, best of 3: 5.85 us per loop

%timeit a=np.empty(10000); a[:]=5
100000 loops, best of 3: 7.15 us per loop

%timeit a=np.ones(10000)*5
10000 loops, best of 3: 22.9 us per loop

%timeit a=np.repeat(5,(10000))
10000 loops, best of 3: 81.7 us per loop

%timeit a=np.tile(5,)
10000 loops, best of 3: 82.9 us per loop
``````

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).

I had `np.array(n * [value])` in mind, but apparently that is slower than all other suggestions for large enough `n`. The best in terms of readability and speed is

``````np.full(n, 3.14)
``````

Here is full comparison with perfplot (a pet project of mine). The two `empty` alternatives are still the fastest (with NumPy 1.12.1). `full` catches up for large arrays.

Code to generate the plot:

``````import numpy as np
import perfplot

def empty_fill(n):
a = np.empty(n)
a.fill(3.14)
return a

def empty_colon(n):
a = np.empty(n)
a[:] = 3.14
return a

def ones_times(n):
return 3.14 * np.ones(n)

def repeat(n):
return np.repeat(3.14, (n))

def tile(n):
return np.repeat(3.14, [n])

def full(n):
return np.full((n), 3.14)

def list_to_array(n):
return np.array(n * [3.14])

perfplot.show(
setup=lambda n: n,
kernels=[empty_fill, empty_colon, ones_times, repeat, tile, full, list_to_array],
n_range=[2 ** k for k in range(27)],
xlabel="len(a)",
logx=True,
logy=True,
)
``````

I believe `fill` is the fastest way to do this.
``````a = np.empty(10)
You should also always avoid iterating like you are doing in your example. A simple `a[:] = v` will accomplish what your iteration does using numpy broadcasting.