Python filter() function

The Python filter() function is an inbuilt function in Python that is used to return the elements of lists or the values in dictionaries corresponding to specific conditions.

def check_value(val):
  return val > 5
my_list = [3, 10, 7, 4, 9, 12]

filtered_list = filter(check_value, my_list)

# -> [10, 7, 9, 12]

The Python filter() function basically applies a boolean test that determines whether the element of list or value of dictionary should be returned. 

The filter() method takes two parameters. First is the function that needs to be applied on items to check whether each item satisfies some conditions and the second is the sequence of items e.g. a list that contains multiple items.

The basic syntax for the filter() method is as below

filter(function, iterable_item)

What is a filter() function

The filter() function is a Python function that takes an iterable object and a callback function as input. This method operates on the items in an iterable object and returns a list of elements from it.

 filter() does something that you can’t do with for loops and list comprehensions.

Example of filter() method

Another example of the filter method here that will filter out all the falsy values like blank string, None from the list, and returns the new list.

def remove_falsy_vals(name):
    return name not in ["", None]
names_list = ["Rick", "", "Carol", "Carl", None]

filtered_list = filter(remove_falsy_vals, names_list)

# -> ['Rick', 'Carol', 'Carl']

Description of above code step by step:

1. We have a list named names_list that has a sequence of items. It contains name string as well as some blank values and null values also.

2. We want to filter out these blank values and null values from the list.

3. We have created a method remove_falsy_vals in our code that takes a value as a parameter and check if it is not equal to blank or None.

4. To check each value of names_list against the above method. We are using the filter() method. We are passing the function remove_falsy_vals and list names_list to it and it will return the filtered list that will not contain any blank or None values.

5. We are getting the result of the filter() function in filtered_list and printing it to check.

Use of lambda function with filter() function

As we can know that we can use functions to filter out our list using the filter() method. We can also use lambda functions in place of functions.

Read more about Lambda functions here

An example of lambda function with filter() functions where will do the same task using lambda function as shown in the previous example

names_list = ["Rick", "", "Carol", "Carl", None]

filtered_list = filter(lambda name : name not in ["", None], names_list)

# -> ['Rick', 'Carol', 'Carl']

Here we have replaced our function with lambda function with a single line of code

lambda name : name not in ["", None]

Well, that’s it! Be sure to check back here soon for more examples on the Python filter() function here.

students = [
  {"name": "Frank", "is_active": False },
  {"name": "Rusell", "is_active": True },
  {"name": "Amit", "is_active": True },
  {"name": "Glenn", "is_active": False }

results = filter(lambda student : student['is_active'] == True, students)

results = list(results)

# -> [{'name': 'Rusell', 'is_active': True}, {'name': 'Amit', 'is_active': True}]
Here, we have a list of dictionaries students that has two keys name and is_actice. We are filtering the list based on is_active key-value True. The filter() method will return the dict items with True value for is_active property.
Was this helpful?