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 see our video player troubleshooting guide to resolve the issue.

Filter Iterables With filter()

00:00 Good news: Python has a whole function dedicated to filtering. Let’s discover it. Using the filter() function is a convenient way to separate and extract data from an iterable based on a given condition.

00:14 Using the filter() function also eliminates the need for developers to write complex filtering logic. The filter() function is a higher-order function, which means it takes another function as one of its parameters.

00:30 The filter() function takes in two parameters: function and iterable. function provides the criteria to filter out uwanted values from the input iterable. As you remember, this function argument is the reason filter() is a higher-order function.

00:48 iterable is any iterable, such as lists, tuples, sets, and iterable objects such as generators. The

00:58 filter() function applies function to every item of the iterable in a loop. The result is an iterator that yields the return values from the function.

01:10 This process doesn’t modify the original input iterable.

01:16 Before we get into using filter() in action, let’s pay attention to something. The first argument to filter() is a function object, which means that you need to pass a function without calling it with a pair of parentheses.

01:32 Let’s extract positive numbers using filter(). So the same problem from before, but instead of if statements and for loops, let’s use filter().

01:42 Your goal is to use filter() to keep the positive numbers and filter out the rest.

01:50 The input is going to be the same, so let’s create a list of numbers from -2 to 2. numbers = [-2, -1, 0, 1, 2].

02:01 By the way, you will probably hear terms like the predicate function or the Boolean function. These are the same as your filtering condition.

02:14 def is_positive(). You’re taking in number as an input.

02:20 Now this function is supposed to get a number as an input and then return True if that number is bigger than 0 and False if it’s not. So return number > 0.

02:31 For example, if you put a -2 here, return -1 > 0. Well, is -2 bigger than 0? No, so the function is going to return False. And if you put in 2, for example, is 2 bigger than 0? Yes, so the function is going to return True.

02:54 Now, let’s use the filter() function with the is_positive function as as its function argument and numbers as its iterable argument. filter is_positive—by the way, pay attention how I’m not using the parentheses here since filter() needs a function object and not a function call—and numbers as its iterable argument.

03:20 Here you can see that you get a filter object, and you can’t really see the results. But why? The reason is that filter() returns an iterator object and not a list, so you can’t just see its results in the console. To do that, let’s convert it into a list. list(filter (is_positive, numbers).

03:48 Here you go. The result is 1 and 2. Let’s go through what happened here. You wrote the is_positive function to take a number as an argument and return True if the number is greater than 0 and otherwise return False. When you call filter(), it applies is_positive to every value in numbers, filtering out the negative numbers. This is why -2, -1, and 0 have been successfully filtered out since their filtering condition, a.k.a. the is_positive function, has evaluated as False.

04:22 And of course, 1 and 2 are bigger than 0, hence why they’re the answer. You just successfully extracted positive numbers of a list using filter(). Way to go!

04:36 Now you might be wondering why would you use this filter() function instead of good old for loops and conditional statements? Well, because the result is shorter.

04:46 The filter() function can be expressed in a single line of code, and the syntax is pretty intuitive. The result produced by filter() is also faster.

04:56 Why? Well, filter() is written in C and is highly optimized, and also its internal implicit loop can be more efficient than a regular for loop regarding execution time.

05:08 This efficiency is arguably the most important advantage of using this function in Python. The result is also more memory efficient. The filter() function returns an iterator, which means it only stores elements in memory as they’re needed.

05:26 You learned a lot of important things in this lesson. You discovered filter() function, learned how to use it, and also understood its advantages over regular for loops and conditional statements.

Become a Member to join the conversation.