Articles → Numpy → Conditionally Selecting The Elements In An Array In Numpy

# Conditionally Selecting The Elements In An Array In Numpy

## Example

```import numpy as np

arr = np.arange(1,10)

# Check the array for numbers greater than 5
result =  arr > 5
print(arr[result])```

1. An array is created using the arange function with value from 1 to 9.
2. Check the value of array greater than 5 using a single expression arr > 5 and put the result of the expression in an array ‘result’.
3. The result will be a boolean array with true and false values
4. Click to Enlarge

5. The value from position 0 to 4 is false, because the value at these positions is less than 5. The value from 5 to 8 is true, because the value at these positions is greater than 5.
6. Finally, printing the value of arr[result], which returns the value from position 5 to 8 (all true values).
7. Click to Enlarge

```import numpy as np

arr = np.arange(1,10)

print(arr[arr> 5])```

```import numpy as np

arr = np.arange(1,10)

# Check the array for even numbers
result =  arr % 2 == 0
even_num_array = arr[result]

# Check the array for odd numbers
result =  arr % 2 != 0
odd_num_array = arr[result]

print("Even array", even_num_array)
print("Odd array", odd_num_array)```

 Posted By  - Karan Gupta Posted On  - Friday, March 29, 2019

Query/Feedback

 Your Email Id Subject Query/Feedback Characters remaining 250