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

PyTorch · Function

torch.where

torch.where selects elements from input or other according to a condition, supporting broadcasting across the operands. With only a condition, it returns the indices of true elements.

How it works

torch.where performs elementwise selection: a true condition chooses input, while a false condition chooses other. The three operands are broadcast together, and the result has that broadcasted shape. With only condition, it instead returns the indices of true elements as a tuple.

Syntax

torch.where(condition, input, other, *, out=None) → Tensor
torch.where(condition) → tuple of LongTensor

Parameters

NameTypeDescription
conditionBoolTensorBoolean tensor; true or nonzero entries choose input, while false entries choose other.
inputTensor or ScalarTensor or scalar chosen where condition is true.
otherTensor or ScalarTensor or scalar chosen where condition is false.
outTensor, optionalOptional destination tensor for the operation.

Returns

For the three-argument form, a Tensor whose shape equals the broadcasted shape of condition, input, and other. The one-argument form returns a tuple of LongTensors.

Key points

  • Use the three-argument form for elementwise conditional selection between two values.
  • A true or nonzero condition selects input; every other condition selects other.
  • input may be one scalar or a tensor supplying values at true-condition positions.
  • other may be one scalar or a tensor supplying values at false-condition positions.
  • The result's dimensions are the broadcasted dimensions of all three operands.
  • The one-argument overload provides true-element coordinates as a tuple of LongTensors.

Common mistakes

  • condition, input, and other must have shapes that can be broadcast together; incompatible shapes raise an error.
  • The one-argument form returns index tensors rather than selected values, matching nonzero with tuple output.

Example: Select values conditionally

import torch

condition = torch.tensor([True, False, True])
values = torch.tensor([1.0, 2.0, 3.0])
result = torch.where(condition, values, torch.tensor(0.0))
print(result)  # tensor([1., 0., 3.])

Selects each element from values when the corresponding condition is true; otherwise it selects zero.

When to use it

Elementwise masking

Replace invalid, masked, or disallowed tensor elements with a fallback value without writing an explicit Python loop.

Conditional tensor transformation

Choose between two tensors or between a tensor and a scalar while preserving a broadcasted tensor layout.

Find true-element indices

Obtain coordinates of entries satisfying a Boolean mask using the one-argument overload.

Interview questions

QWhat does torch.where do, and what shape does it return?

torch.where(condition, input, other) returns an elementwise tensor. At each position it takes input when condition is true and other otherwise; the operands must be broadcastable.

Follow-up: How can torch.where return indices? Use the one-argument form torch.where(condition), which is equivalent to torch.nonzero(condition, as_tuple=True).

QWhat types can the operands have?

condition is a BoolTensor: true or nonzero entries select input, and false entries select other. input and other may each be tensors or scalars.

Follow-up: What determines the output shape? Broadcasting condition, input, and other determines the common output shape.

QWhat are the optional output argument and return value?

The optional out keyword supplies the output tensor. The returned tensor has the shape obtained by broadcasting condition, input, and other.

Follow-up: What alternative tensor method is documented? self.where(condition, y) is equivalent to torch.where(condition, self, y).

  • torch.nonzero
  • torch.Tensor.where

Source: torch.where. Checked 2026-10-16.