🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to NumPy Notes
Topic #16

NumPy Array Filter


Filtering Arrays

Getting some elements out of an existing array and creating a new array out of them is called filtering.

In NumPy, you filter an array using a boolean index list.

Note: A boolean index list is a list of booleans corresponding to indexes in the array.

If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.

Example

  import numpy as np

arr = np.array([41, 42, 43, 44])

x = [True,
  False, True, False]

newarr = arr[x]

print(newarr)

The example above will return [41, 43], why?

Because the new array contains only the values where the filter array had the value True, in this case, index 0 and 2.


Creating the Filter Array

In the example above we hard-coded the True and False values, but the common use is to create a filter array based on conditions.

Example

import numpy as np

arr = np.array([41, 42, 43, 44])

#
Create an empty list
filter_arr = []

# go through each element in
arr
for element in arr:
  # if the element is higher than 42, set
the value to True, otherwise False:
  if element > 42:

filter_arr.append(True)
  else:
    filter_arr.append(False)

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)

Example

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

#
Create an empty list
filter_arr = []

# go through each element in
arr
for element in arr:
  # if the element is completely divisble
by 2, set the value to True, otherwise False
  if element % 2 == 0:

filter_arr.append(True)
  else:
    filter_arr.append(False)

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)

Creating Filter Directly From Array

The above example is quite a common task in NumPy and NumPy provides a nice way to tackle it.

We can directly substitute the array instead of the iterable variable in our condition and it will work just as we expect it to.

Example

  import numpy as np

arr = np.array([41, 42, 43, 44])

  filter_arr = arr
  > 42

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)

Example

  import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

filter_arr = arr
  % 2 == 0

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)

Want to go beyond the notes?

Join CodingNow 2.0's NumPy course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

NumPy Array Filter – FAQs

Quick answers about learning NumPy Array Filter in NumPy.

This free note from CodingNow 2.0 explains NumPy Array Filter in NumPy — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every NumPy topic on CodingNow 2.0, including NumPy Array Filter, is 100% free with no signup required.
With focused practice, most students grasp NumPy Array Filter in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now