In this example we will show how to create a filter array that will return only even elements from the original array using the for loop in Python NumPy.
Source Code
import numpy as np
n = np.array([5, 6, 7, 8, 9, 10, 11, 12])
# Create an empty list to store Boolean values.
filter_boolean = []
for item in n:
# if the element is completely divisible by 2, set the value to True, otherwise False
if item % 2 == 0:
filter_boolean.append(True)
else:
filter_boolean.append(False)
filter_n = n[filter_boolean]
print(filter_boolean)
print(filter_n)
Output:
[False, True, False, True, False, True, False, True]
[ 6 8 10 12]