NumPy zeros_like: Create a Zero Array from a Template

When the next calculation needs an array of zeros that lines up with an array you already have, NumPy can build it for you. np.zeros_like creates that array, copying the shape and data type of the one you give it.

I’ll show you how to override the dtype or shape, and when plain np.zeros fits better.

What is NumPy zeros_like?

NumPy’s zeros_like function creates a zero-filled array from an existing array, carrying over its shape and data type unless you override either property.

That makes zeros_like useful when the next calculation needs a zero array aligned with an array you already have. NumPy’s zeros function is the alternative when you want to specify the dimensions directly.

Default from the inputMeaning
shapeThe output dimensions match the template.
dtypeThe output uses the template’s element type.

Prerequisites

The examples assume Python, NumPy, and basic array creation.

  • Import NumPy with the conventional np alias.
  • Use zeros_like when an existing array should define the output shape and default data type.

Step 1: Create zeros from an existing array

Use the input array as the template when each zero should line up with one existing element. This example starts with a 2-by-3 integer array, then prints both arrays so you can compare their dimensions.

import numpy as np

values = np.arange(6).reshape(2, 3)
zeros = np.zeros_like(values)

print("template:\n", values)
print("zeros:\n", zeros)
print("shape and dtype:", zeros.shape, zeros.dtype)

The result has shape (2, 3) and dtype int64, inherited from values. I ran this example with NumPy 2.4.6, and the printed zero array kept both rows and all three columns.

NumPy zeros_like output for a 2 by 3 array and dtype and shape overrides
A tested zeros_like call keeps the input shape and allows deliberate dtype and shape overrides.

Step 2: Override the dtype or shape

Pass dtype when the zero values need a different numeric type, or shape when the result needs different dimensions. Both options override the corresponding property that would otherwise come from the template.

CallEffect
np.zeros_like(values)Uses values.shape and values.dtype
np.zeros_like(values, dtype=np.float64)Keeps the shape and uses float64
np.zeros_like(values, shape=(3, 2))Uses the supplied shape and the input dtype
float_zeros = np.zeros_like(values, dtype=np.float64)
reshaped_zeros = np.zeros_like(values, shape=(3, 2))

print(float_zeros.dtype)
print(reshaped_zeros.shape)

The output is float64 for the first call and (3, 2) for the second. The shape override changes the dimensions, so it no longer matches the template even though the input still supplies the default dtype.

Choose zeros_like or zeros

Choose zeros_like when an existing array should supply the shape and, unless overridden, the dtype. Choose zeros when you already know the dimensions and want to state them directly.

  • Template array available: use zeros_like(template).
  • Dimensions known independently: use zeros(shape).

For example, zeros_like(values) follows the array’s current dimensions, while zeros((2, 3)) spells out the requested dimensions. See the NumPy zeros guide for the shape-based call.

Keep the template’s metadata in view

The input does more than provide a count of elements. Its shape and dtype set the defaults, so integer input produces integer zeros unless you pass another dtype.

NumPy also accepts order to control memory layout, subok to choose whether to preserve an input subclass, and device for Array API interoperability. The current NumPy reference says device must be cpu when supplied, so do not treat this argument as general GPU placement.

check = np.zeros_like(values)
print(check.shape == values.shape)
print(check.dtype == values.dtype)

Both checks print True for the default call. Keep the template when its metadata expresses the output you need, and pass only the overrides that should differ.

NumPy zeros_like questions

What does NumPy zeros_like do?

It creates an array filled with zeros, using an input array’s shape and data type by default.

How do I make zeros_like return floats?

Pass a floating-point dtype, such as dtype=np.float64, to override the input array’s data type.

How is zeros_like different from zeros?

zeros_like takes an array template and inherits its shape and dtype by default. zeros takes the requested shape directly.

Tanvi Bugdani
Tanvi Bugdani
Articles: 63