What is the filter() method in Python?

Arun
3 min readNov 21, 2022
Photo by James A. Molnar on Unsplash

The filter() method in Python is a built-in function that takes in an iterable object (like a list, set, tuple, etc.) and a function as arguments. The function passed into filter() must return a Boolean value (either True or False). The filter() method then returns a new iterable object containing only the elements from the original iterable object that returned True when passed into the function.

filter(function, iterable)

The filter() method takes two arguments:

function — function that tests if elements of an iterable returns true or false

iterable — iterable which needs to be filtered, could be sets, lists, tuples, or containers of any iterators

The filter() method returns an iterator that passed the function check for each element in the iterable.

If no function is specified, then the identity function is assumed, and all elements of the iterable that are false are removed.

In other words, the filter() method allows you to “filter” out elements from an iterable object that don’t meet certain criteria. Let’s take a look at a few examples to see how this works.

Example 1: Filtering out odd numbers from a list

Let’s say we have a list of numbers and we want to create a new list that contains only the even numbers from the original list. We can do this using the filter() method and a lambda function (a small anonymous function) that returns True if the number passed into it is even and False otherwise.

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list_iterator = filter(lambda x: x % 2 == 0, original_list)
print(new_list_iterator)

# Output: <filter object at 0x112e2ff10>

The filter() method returns an iterator, so by passing the list function we can see the filtered elements.

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list = list(filter(lambda x: x % 2 == 0, original_list))
print(new_list)

# Output: [2, 4, 6, 8, 10]

Example 2: Filtering out strings from a list

We can also use the filter() method to filter out elements that are not of a certain data type. For example, let’s say we have a list of mixed data types and we want to create a new list containing only the strings from the original list. We can do this by passing in a lambda function that checks if the element is of type string.

original_list = [1, 'a', 'b', 'c', 2, 3]
new_list = list(filter(lambda x: type(x) == str, original_list))
print(new_list)

# Output: ['a', 'b', 'c']

Example 3: Filtering out numbers greater than 10

num_list = [4, 5, 6, 7, 8, 9, 10, 11, 12] 

def greater_than_ten(num):
return num > 10

newList = list(filter(greater_than_ten, num_list))
print(newList)

# Output: [11, 12]

Conclusion:

As you can see from these examples, the filter() method can be quite handy when you need to create a new iterable object based on another iterable object. It’s important to note that the filter() method does not modify the original iterable object — it simply creates a new one based on the criteria specified in the function passed into it.

--

--