# 6 ways to fill NumPy array with all same values

In this post, we will learn about 6 ways to fill NumPy array with all same values. To create a NumPy array of all same values in python the first step is to create an array of given shapes that contain only identical values. We will use different Numpy library functions. We can achieve this with slicing or functions.

### 6 ways to fill NumPy array with all same values in Python

• Fill NumPy array with all same values using slice
• Fill NumPy array with all same values using full()
• Fill NumPy array with all same values using fill()
• Fill NumPy array with all same values using tile()
• Fill NumPy array with all same values using ones()
• Fill NumPy array with all same values using full_like()

### 1. Fill NumPy array with all same values using slice

In this Python program, we have created a numpy empty array by using the empty() function of a given shape(2,3) to assign all same values to the numpy array element slicing [:] select all elements of the array regardless of dimensions and replace with the given value.

The assigned value should be of the same type as the array otherwise it raises an error.to change the type astype() method can be used.

```import numpy as np

nparr = np.empty((2,3))

nparr[:] = 15
print(nparr,'\n')

nparr = 15 * np.ones(shape=(2,3))

#assign same float value to Numpy array
nparr = nparr.astype(np.float)
nparr[:] = 0.5

print('float type array:\n',nparr)

```

Output

```[[15. 15. 15.]
[15. 15. 15.]]

float type array:
[[0.5 0.5 0.5]
[0.5 0.5 0.5]]
```

### 2. Fill NumPy array with all same values using full()

The numpy. full() return an array of given shapes after filling the same values in all elements of the array. In this Python program, we have created an array of shapes (2,3) and filled it with value15.

#### Python program to fill NumPy array with all same values using full()

```import numpy as np

nparr = np.full((2,3), 15)

print(nparr)

```

Output

```[[15 15 15]
[15 15 15]]
```

### 3. Fill NumPy array with all same values using fill()

The numpy. ndarray.fill() function Fill the array with a scalar value. In this python program, we have created a numpy empty array of shapes (2,3) and by using the fill() function filled it with value 15.

#### Python Program to Fill NumPy array with all same values using fill()

```import numpy as np

nparr=np.empty((2,3))
nparr.fill(15)

print(nparr)

```

Output

```[[15. 15. 15.]
[15. 15. 15.]]
```

### 4. Fill NumPy array with all same values using tile()

The numpy tile function returns a new array by simply repeating a number of elements in the array as per given reputations. In this python program, we have repeated the value 25 for given reputations (2,3).

#### Python program create numpy array with same values

```import numpy as np

rows = 2
cols = 3

nparr = np.tile(15, (rows,cols))

print(nparr)
```

Output

```[[15 15 15]
[15 15 15]]
```

### 5. Fill Numpy array with same values using ones()

In this Python program, we have used the np. ones() function to create a numpy array of a given shape(3,2) and that has the same identical value 15 for each element.

#### Python Program to create numpy array with same values using ones()

```import numpy as np

nparr = 15 * np.ones(shape=(3,2))

print(nparr)

```

Output

```[[15. 15. 15.]
[15. 15. 15.]]
```

### 6. Fill Numpy array with same values using full_like()

The numpy.full_like() returns a full array of given shape and size as passed array. We will use it to create an numpy array with same all values.

#### Python program create numpy array with same values

```import numpy as np

nparr = np.arange(6, dtype = int).reshape(2, 3)
print("Created array using reshape() : \n", nparr)

print("\n after full_like : \n", np.full_like(nparr, 15))
```

Output

```Created array using reshape() :
[[0 1 2]
[3 4 5]]

after full_like :
[[10 10 10]
[10 10 10]]
```

## Summary

In this post, we have learned 6 ways to fill the NumPy array with all same values in Python.