# NumPy: numpy.full_like() function

## numpy.full_like() function

The numpy.full_like() function is used to create an array with the same shape as an existing array and fill it with a specific value.

Syntax:

`numpy.full_like(a, fill_value, dtype=None, order='K', subok=True)` Parameters:

Name Description Required /
Optional
a The shape and data-type of a define these same attributes of the returned array. Required
fill_value Fill value. Required
dtype Overrides the data type of the result. optional
order Overrides the memory layout of the result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if a is Fortran contiguous, 'C' otherwise. 'K' means match the layout of a as closely as possible. optional
subok If True, then the newly created array will use the sub-class type of 'a', otherwise it will be a base-class array. Defaults to True. optional

Return value:

Array of fill_value with the same shape and type as a.

Example: Create arrays of the same shape as another array with np.full_like

``````>>> import numpy as np
>>> a = np.arange(5, dtype=int)
>>> np.full_like(a, 1)
array([1, 1, 1, 1, 1])
>>> np.full_like(a, 0.1)
array([0, 0, 0, 0, 0])
>>> np.full_like(a, 0.1, dtype=float)
array([ 0.1,  0.1,  0.1,  0.1,  0.1])
>>> np.full_like(a, np.nan, dtype=np.double)
array([ nan,  nan,  nan,  nan,  nan])
``````

In the said code the first example fills the array with integer value 1, while the second example fills it with a float value 0.1.
The second example produces an array filled with 0, not 0.1, because the data type of a is integer and the default data type of np.full_like is the same as the input array.
In the third example, dtype is explicitly set to float to get an array with float values. Here, dtype is set to np.double and the value of np.nan is used to create an array with NaN (not a number) values of double precision.

Pictorial Presentation:  Example: Create a numpy array filled with a constant value using np.full_like()

``````>>> import numpy as np
>>> b = np.arange(5, dtype=np.double)
>>> np.full_like(b, 0.5)
array([ 0.5,  0.5,  0.5,  0.5,  0.5])
``````

In the above code, an array 'b' is created using the np.arange() function with a data type of 'double'. Then, the np.full_like() function is called with 'b' as the first argument, and a constant value of 0.5 as the second argument. This creates a new numpy array of the same shape and data type as 'b', filled with the constant value 0.5.

Python - NumPy Code Editor:

Previous: full()
Next: From existing data array()

﻿