numpy.select() function - Python Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The numpy.select() function is used to construct an array by selecting elements from a list of choices based on multiple conditions. It is particularly useful when dealing with conditional replacements or transformations in NumPy arrays. Example: Python import numpy as np arr = np.array([10, 20, 30, 40]) conditions = [arr < 20, arr > 30] choices = [100, 200] result = np.select(conditions, choices, default=0) print(result) Syntax:numpy.select(condlist, choicelist, default=0)Parameters:condlist : list of bool ndarrays A list of boolean NumPy arrays that determine from which array in choicelist the output elements are selected. If multiple conditions are True, the first one encountered is used.choicelist : list of ndarrays A list of arrays from which the output elements are chosen. It must have the same length as condlist.default : scalar, optional (default=0) The value inserted in the output array when none of the conditions are met.Return Value:ndarray : An array with elements chosen from choicelist based on the conditions in condlist.Code Implementation 1. Basic Usage of numpy.select() Here:If arr < 3, the corresponding element is taken from arr.If arr > 4, the corresponding element is taken from arr**3.Otherwise, the default value 0 is used. Python import numpy as np arr = np.arange(8) condlist = [arr < 3, arr > 4] choicelist = [arr, arr**3] result = np.select(condlist, choicelist) print(result) Output :[ 0 1 2 0 0 125 216 343]2. Using a Different Default ValueHere:Values where arr < 4 are taken from arr.Values where arr > 6 are taken from arr**2.All other values are replaced with -1 (instead of 0). Python arr = np.arange(8) condlist = [arr < 4, arr > 6] choicelist = [arr, arr**2] # Custom default value (e.g., -1) result = np.select(condlist, choicelist, default=-1) print(result) Output: 0 1 2 3 -1 -1 -1 49]3. Handling Multiple ConditionsHere :If arr is even (arr % 2 == 0), it is multiplied by 10.If arr is divisible by 3 (arr % 3 == 0), it is negated.If neither condition is met, the default value 100 is used. Python arr = np.arange(10) condlist = [arr % 2 == 0, arr % 3 == 0] choicelist = [arr * 10, arr * -1] result = np.select(condlist, choicelist, default=100) print(result) Output: [ 0 100 20 -3 40 100 60 100 80 -9]Why Use numpy.select()?More flexible than numpy.where() when dealing with multiple conditions.Helps avoid complex nested if-else conditions in array transformations.Efficient and concise for applying different transformations to an array based on conditions.Comparison with numpy.where()Featurenumpy.select()numpy.where()Multiple ConditionsYesNo (only two conditions: True/False)Custom Default ValueYesNoSimplicityBetter for multiple rulesBetter for simple if-elseThe numpy.select() function is a powerful tool for conditional selection and transformation of array elements. It is especially useful when handling multiple conditions efficiently in a structured way. Mastering its usage will help simplify complex array operations in Python. Comment More info S sanjoy_62 Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like