Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.zeros_like

Creates a new array filled with zeros, using an input array-like object's shape and data type by default. Optional arguments can override the dtype, layout, subclass handling, shape, or device.

How it works

numpy.zeros_like builds a new zero-filled array using the input's shape and data type by default. Optional arguments can change the output dtype, memory order, subclass behavior, shape, and device.

Syntax

numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None, like=None)

Parameters

NameTypeDescription
aarray_likeArray-like object whose shape and data type provide the defaults for the result.
dtypedata-type, optionalOutput data type; if omitted, the type is inferred from a.
order{'C', 'F', 'A', or 'K'}, optionalMemory layout of the result: 'C', 'F', 'A', or 'K'.
subokbool, optionalIf true, preserve an input ndarray subclass; otherwise return a base ndarray.
shapeint or sequence of ints, optionalShape of the result. If omitted, the shape of a is used.
devicestr, optionalDevice on which to place the result; relevant to array-API-compatible creation.
likearray_like, optionalArray-API-compatible namespace used for array creation.

Returns

An ndarray filled with zeros, using the requested or inferred shape, data type, memory order, subclass behavior, and device.

Key points

  • The input's shape and data type determine the corresponding properties of the result.
  • Use dtype to request an output type different from the input's type.
  • The order option selects the returned array's memory arrangement.
  • The optional shape argument specifies the dimensions of the returned array.

Common mistakes

  • Passing dtype replaces the input array's data type, so the result may no longer match the input dtype.
  • The order argument can change how the returned array is laid out in memory.

Example: Create a matching zero array

import numpy as np

values = np.arange(6, dtype=np.float32).reshape(2, 3)
zeros = np.zeros_like(values)
print(zeros)
print(zeros.dtype)  # float32

Creates a zero-filled array matching values in shape and dtype.

When to use it

Shape- and dtype-compatible initialization

Allocate a zero buffer matching an existing tensor or ndarray.

Algorithm workspace

Prepare a same-sized accumulator before numerical processing.

Controlled array allocation

Create a placeholder that follows a chosen memory layout or dtype.

Interview questions

QWhat does numpy.zeros_like do?

It returns a zero-filled array whose shape and data type are taken from a supplied array-like object. The returned array is newly created rather than a view of the input.

Follow-up: How would you force a different element type? Pass dtype, such as dtype=np.float64; this overrides the input type.

QHow is the output dtype chosen?

The default dtype is inferred from a, while an explicit dtype overrides that inference. This lets the function preserve an existing numeric type or deliberately choose another one.

Follow-up: What happens with a complex input and dtype=float? The result uses float, so complex information is not represented in the output dtype.

QWhat do order and shape control?

order controls the result's memory layout using C, F, A, or K. The default K attempts to match the input's layout as closely as possible.

Follow-up: When might shape matter? Supplying shape changes the output dimensions; if omitted, the input shape is used.

  • numpy.ones_like
  • numpy.full_like
  • numpy.empty_like
  • numpy.zeros

Source: numpy.zeros_like. Checked 2026-10-15.