numpy.where() in Python Last Updated : 30 Sep, 2025 Comments Improve Suggest changes 22 Likes Like Report numpy.where() is used for conditional selection and replacement in NumPy arrays. It can be used to:Find indices that satisfy a conditionBuild a new array by choosing values from two options depending on a condition.In this example, np.where() is used with only a condition to get the indices where elements are greater than 20. Python import numpy as np arr = np.array([10, 15, 20, 25, 30]) result = np.where(arr > 20) print(result) Output(array([3, 4]),) Explanation:arr > 20 produces a boolean mask: [False, False, False, True, True].np.where(...) with a single argument returns a tuple containing the indices of True entries.The returned value (array([3, 4]),) indicates elements at positions 3 and 4 satisfy the condition.Syntax:numpy.where(condition[, x, y])Parameters:condition: boolean array (or expression producing one).x (optional): array-like or scalar used where condition is True.y (optional): array-like or scalar used where condition is False.Return value:With only condition: returns a tuple of index arrays (one per dimension).With x and y: returns a new array choosing from x where True, y where False (supports broadcasting and dtype rules).Uses of numpy.where()1. Find indices that satisfy a conditionIn this example, numpy.where() checks where the condition arr % 2 == 0 is true and returns the indices. Python import numpy as np arr = np.array([2, 7, 8, 3, 10]) idx = np.where(arr % 2 == 0) print(idx) print("Even elements:", arr[idx]) Output(array([0, 2, 4]),) Even elements: [ 2 8 10] Explanation:arr % 2 == 0 creates a boolean mask for even numbers.np.where(...) returns indices of True values: (array([0,2,4]),).arr[idx] uses those indices to extract the even elements.2. Binary replacement with scalars (x and y) By providing x and y as arguments, you can use numpy.where() to return different values depending on whether condition is true or false.Here, numpy.where() function checks the condition arr > 20. Python import numpy as np arr = np.array([10, 15, 20, 25, 30]) result = np.where(arr > 20, 1, 0) print(result) Output[0 0 0 1 1] Explanation:arr > 20 builds the boolean mask.np.where(mask, 1, 0) returns an array with 1 where mask is True, else 0.Result is an integer array representing the binary condition.3. Pick from two arrays elementwiseIn this example, for elements where condition arr1 > 20 is true, corresponding element from arr1 is chosen. Otherwise, element from arr2 is selected. Python import numpy as np arr1 = np.array([10, 15, 20, 25, 30]) arr2 = np.array([100, 150, 200, 250, 300]) result = np.where(arr1 > 20, arr1, arr2) print(result) Output[100 150 200 25 30] Explanation:arr1 > 20 identifies where to take values from arr1 (True for indices 3 and 4).np.where(condition, arr1, arr2) returns values from arr1 where True, otherwise from arr2.Final array picks arr2 values where condition is False (indices 0–2) and arr1 values where condition is True (indices 3–4). 4. Replace negatives with zeroThis example replaces negative numbers in a 2-D array with 0. Python import numpy as np mat = np.array([[ 5, -2, 3], [-1, 4, -6]]) result = np.where(mat < 0, 0, mat) print(result) Output[[5 0 3] [0 4 0]] Explanation:mat < 0 makes a boolean matrix of the same shape.np.where(..., 0, mat) returns a new array where negatives are replaced by 0, others kept.The function broadcasts scalars (0) across the array shape. Comment A ArkadipGhosh Follow 22 Improve A ArkadipGhosh Follow 22 Improve Article Tags : Numpy Python-numpy Python numpy-Indexing Explore NumPy Tutorial - Python Library 3 min read IntroductionNumPy Introduction 6 min read Python NumPy 6 min read NumPy Array in Python 2 min read Basics of NumPy Arrays 4 min read Numpy - ndarray 3 min read Data type Object (dtype) in NumPy Python 3 min read Creating NumPy ArrayNumpy - Array Creation 5 min read numpy.arange() in Python 2 min read numpy.zeros() in Python 2 min read NumPy - Create array filled with all ones 2 min read NumPy - linspace() Function 2 min read numpy.eye() in Python 2 min read Creating a one-dimensional NumPy array 2 min read How to create an empty and a full NumPy array 2 min read Create a Numpy array filled with all zeros - Python 2 min read How to generate 2-D Gaussian array using NumPy? 2 min read How to create a vector in Python using NumPy 4 min read Python - Numpy fromrecords() method 2 min read NumPy Array ManipulationNumPy Copy and View of Array 4 min read How to Copy NumPy array into another array? 2 min read Appending values at the end of an NumPy array 4 min read How to swap columns of a given NumPy array? 4 min read Insert a new axis within a NumPy array 4 min read numpy.hstack() in Python 2 min read numpy.vstack() in python 2 min read Joining NumPy Array 3 min read Combining a One and a Two-Dimensional NumPy Array 3 min read Numpy np.ma.concatenate() method-Python 2 min read Numpy dstack() method-Python 2 min read Splitting Arrays in NumPy 6 min read How to compare two NumPy arrays? 2 min read Find the union of two NumPy arrays 2 min read Find unique rows in a NumPy array 3 min read Numpy np.unique() method-Python 2 min read numpy.trim_zeros() in Python 2 min read Matrix in NumPyMatrix manipulation in Python 4 min read numpy matrix operations | empty() function 1 min read numpy matrix operations | zeros() function 2 min read numpy matrix operations | ones() function 2 min read numpy matrix operations | eye() function 2 min read numpy matrix operations | identity() function 1 min read Adding and Subtracting Matrices in Python 2 min read Matrix Multiplication in NumPy 2 min read Numpy ndarray.dot() function | Python 2 min read NumPy | Vector Multiplication 4 min read How to calculate dot product of two vectors in Python? 3 min read Multiplication of two Matrices in Single line using Numpy in Python 3 min read Numpy np.eigvals() method - Python 1 min read How to Calculate the Determinant of a Matrix using NumPy 2 min read Python | Numpy matrix.transpose() 3 min read Python | Numpy matrix.var() 1 min read Compute the inverse of a matrix using NumPy 2 min read Operations on NumPy ArrayNumpy | Binary Operations 8 min read Numpy | Mathematical Function 9 min read Numpy - String Functions & Operations 5 min read Reshaping NumPy ArrayReshape NumPy Array 5 min read Python | Numpy matrix.resize() 1 min read Python | Numpy matrix.reshape() 1 min read NumPy Array Shape 2 min read Change the dimension of a NumPy array 3 min read numpy.ndarray.resize() function - Python 1 min read Flatten a Matrix in Python using NumPy 1 min read numpy.moveaxis() function | Python 2 min read numpy.swapaxes() function - Python 2 min read Python | Numpy matrix.swapaxes() 1 min read numpy.vsplit() function | Python 2 min read numpy.hsplit() function | Python 2 min read Numpy MaskedArray.reshape() function | Python 3 min read Python | Numpy matrix.squeeze() 1 min read Indexing NumPy ArrayBasic Slicing and Advanced Indexing in NumPy 5 min read numpy.compress() in Python 2 min read Accessing Data Along Multiple Dimensions Arrays in Python Numpy 3 min read How to Access Different Rows of a Multidimensional NumPy Array 2 min read numpy.tril_indices() function | Python 1 min read Arithmetic operations on NumPyArrayNumPy Array Broadcasting 6 min read Estimation of Variable | set 1 3 min read Python: Operations on Numpy Arrays 3 min read How to use the NumPy sum function? 4 min read numpy.divide() in Python 3 min read numpy.inner() in python 1 min read Absolute Deviation and Absolute Mean Deviation using NumPy | Python 3 min read Calculate standard deviation of a Matrix in Python 2 min read numpy.gcd() in Python 2 min read Linear Algebra in NumPy ArrayNumpy | Linear Algebra 6 min read Get the QR factorization of a given NumPy array 2 min read How to get the magnitude of a vector in NumPy? 3 min read How to compute the eigenvalues and right eigenvectors of a given square array using NumPY? 2 min read Like