26

This is surely an easy question:

How does one create a numpy array of N values, all the same value?

For instance, numpy.arange(10) creates 10 values of integers from 0 to 9.

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

I would like to create a numpy array of 10 values of the same integer,

array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
6
  • 6
    numpy.reapeat(3, 10)... Commented Jul 7, 2015 at 14:11
  • 2
    ...or np.full(10, 3). Commented Jul 7, 2015 at 14:12
  • 1
    @Ashwini Chaudhary you certainly mean repeat() (with only one 'a') ;) Commented Jul 7, 2015 at 14:19
  • @ajcr to be closer to OP question I would suggest to constrain type of the array: np.full(10, 3, dtype=np.int) otherwise it may be float result... Commented Jul 7, 2015 at 14:22
  • 3
    Nice functions, I didn't know them, I always used something like np.ones(10) * 3 . Commented Jul 7, 2015 at 14:37

6 Answers 6

40

Use numpy.full():

import numpy as np

np.full(
  shape=10,
  fill_value=3,
  dtype=np.int
)
    
> array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
Sign up to request clarification or add additional context in comments.

1 Comment

for anyone curious about matrices: arr = np.full(shape=(5,5), fill_value=3, dtype=np.int64)
8

Very easy 1)we use arange function :-

arr3=np.arange(0,10)
output=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

2)we use ones function whose provide 1 and after we will do multiply 3 :-

arr4=np.ones(10)*3
output=array([3., 3., 3., 3., 3., 3., 3., 3., 3., 3.])

Comments

7

An alternative (faster) way to do this would be with np.empty() and np.fill():

import numpy as np

shape = 10
value = 3


myarray = np.empty(shape, dtype=np.int)
myarray.fill(value)

Time comparison

The above approach on my machine executes for:

951 ns ± 14 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

vs using np.full(shape=shape, fill_value=value, dtype=np.int) executes for:

1.66 µs ± 24.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

vs using np.repeat(value, shape) executes for:

2.77 µs ± 41.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

vs using np.ones(shape) * value executes for:

2.71 µs ± 56.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

I find it to be consistently a little bit quicker.

Comments

5

I realize it's not using numpy, but this is very easy with base python.

data = [3]*10
print(data)
> [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

Comments

0

Try this. It is worked well in my python version (Python 3.9.6).

import numpy as np 
np.array([3]*10)

output: 

Comments

-1

In my opinion, this is the easiest way:

import numpy as np

arr = np.arange(0, 11)
arr[:] = 0

output:
[0 0 0 0 0 0 0 0 0 0 0]

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.