How to Use the Method method returns an array drawn from elements in choicelist, depending on conditions.

Syntax, choicelist, default=0)


  1. condlist: list of bool arrays or boolean conditions. The list of conditions to be evaluated.
  2. choicelist: list of arrays. The list of actions (values) to apply for each condition. It must have the same length as the condlist.
  3. default: scalar, optional. The value to use for positions where none of the conditions are True. The default is 0.

Return value

Depending on conditions, it returns an array drawn from elements in a choicelist.

Example 1: How to Use method

import numpy as np

arr = np.arange(6)

condlist = [arr < 5, arr > 1]
choicelist = [arr, arr**2]

output_arr =, choicelist)



[ 0 1 2 3 4 25]

Example 2: Applying conditions and actions 

import numpy as np

# Sample array
arr = np.array([1, 4, 7, 10, 13])

# Define conditions and corresponding actions
conditions = [
  (arr < 5),
  (arr >= 5) & (arr <= 10),
  (arr > 10)

actions = [
  arr * 2,
  arr - 3

# Apply conditions and actions
result =, actions)

print("Original array:", arr)
print("New array with applied conditions:", result)


Original array: [ 1 4 7 10 13]
New array with applied conditions: [ 2 8 7 10 10]

You can see that we created an array and defined three conditions and corresponding actions using lists.

The function applies the conditions and actions, creating a new array with the same shape as the input array.

The values of the new array are determined by the first condition, True, at a given position. If no condition is True, the corresponding value in the new array will be 0 (the default fill value).

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.