Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

How to Use the filter() Function

In this lesson, you’ll see how to use filter(). The built-in function filter() is another classic functional construct and can be converted into a list comprehension.

It takes a predicate as a first argument and an iterable as a second argument. It builds an iterator containing all the elements of the initial collection that satisfies the predicate function.

00:02 Next is filter(). filter() takes two arguments, a function and an iterable. It constructs a new iterator from those elements of the iterable for which the function returns True.

00:17 This allows quick creation of iterables of items in another iterable that have passed a test. Here’s an example where we’re passing it the list of numbers [1, 2, 3, 4], and the lambda expression is only True when division by 2 has no remainder—in other words, when the numbers are even. As a result, we create the new list even, which only has the numbers 2 and 4 in it.

00:44 Next you’ll see filter() in action. So here, you will see a first example of using filter(). We’re going to start out with a list, using range() to fill that, so we’ll have the numbers 1 to 20, and we’ll print that out so you’ll see it. And now comes the filtering.

01:10 So. We’re going to create a new list called evens and use filter() with a lambda expression. lambda x is True when x % 2 == 0.

01:30 That detects whether it’s even or not. And we also pass in the list nums. Now we’re going to print evens out, and then running that, you’ll see how that works. So there you can see the two lists. The first one is all the numbers from 1 to 20, and the second new list is one created by filter() and only contains the numbers where the lambda expression x % 2 == 0 is True.

02:02 In this case, that’s the even numbers. We can extend that idea a little. Make a new list as well, where x % 2 != 0.

02:26 And let’s see that running. And there you can see the third list contains only the odd numbers. The expression was True, but this time not for equality with 0, but inequality with 0.

02:44 Here’s a second example of using filter(), where you’re going to see checking for the value being above the mean of a list of values. We’ll start out by importing the mean() function from the statistics module, and now create our data.

03:05 We’ll calculate the average using mean(),

03:13 and then create our above average list using filter() and a lambda expression.

03:26 And finally, we’ll print out our original list, the average value, and our new list. Let’s see that program in action. And there you see the list 1 to 20, the average value of 10.5, and then our new list of the values that are above the average.

03:56 Again, that could be extended to give us a below average list using a new lambda expression.

04:14 And running it produces what we’d expect, with two new lists—one with the above average values and one with the below average values. The previous filter() examples you’ve seen have been working on numbers. However, the lambda expression can work on any kind of input, so here you’re going to see an example with it working on text. Firstly, we have our list of input data—

04:57 in this case, baseball team nicknames. We’ll print that original list out, and now create a new list using filter().

05:19 Here, the lambda expression is going to be True when the length of x is less than 6.

05:37 Finally, we’ll pass in the nicknames list, and our expression is complete. Now we can print out the new list of names, and let’s see that code running.

05:55 Here’s the original list, and you can see that the only elements of that where the lambda expression was True and the length was less than 6 was 'Cubs' and 'Reds', and those make up the new list.

John Laudun on Sept. 3, 2019

This has been really useful for someone like me who is slowly making his way from novice to something like apprentice with aspirations for craftsman. I am curious about one very small bit: if the goal of lambda functions is to have things in one line, why not above_avg = list(filter(lambda x : x > mean(nums), nums))?

blu on Sept. 5, 2019

i suspect because mean(nums) would be evaluated len(nums) times

Become a Member to join the conversation.