Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Filter Iterables With filter()
00:00Good news: Python has a whole function dedicated to filtering.Let’s discover it.Using thefilter() function is a convenient way to separate and extract datafrom an iterable based on a given condition.
00:14Using thefilter() function also eliminates the need for developers to writecomplex filtering logic.Thefilter() function is a higher-order function,which means it takes another function as one of its parameters.
00:30Thefilter() function takes in two parameters:function anditerable.function provides the criteria to filter outuwanted values from the input iterable. As you remember,thisfunction argument is the reasonfilter() is a higher-order function.
00:48iterable is any iterable, such as lists, tuples, sets,and iterable objects such as generators. The
00:58filter() function appliesfunction to every item of the iterable in a loop.The result is an iterator that yields the return values from the function.
01:10This process doesn’t modify the original input iterable.
01:16Before we get into usingfilter() in action, let’s pay attention to something.The first argument tofilter() is a function object,which means that you need to pass a function without calling it with a pair ofparentheses.
01:32Let’s extract positive numbers usingfilter().So the same problem from before, but instead ofif statements andfor loops,let’s usefilter().
01:42Your goal is to usefilter() to keep the positive numbers and filter out the rest.
01:50The input is going to be the same,so let’s create a list of numbers from-2 to2.numbers =[-2, -1, 0, 1, 2].
02:01By the way,you will probably hear terms like thepredicate function or theBooleanfunction. These are the same as your filtering condition.
02:14def is_positive(). You’re taking innumber as an input.
02:20Now this function is supposed to get a number as an input and then returnTrueif that number is bigger than0 andFalseif it’s not. Soreturn number > 0.
02:31For example, if you put a-2 here,return -1 > 0. Well, is-2 bigger than0? No,so the function is going to returnFalse. And if you put in2,for example, is2 bigger than0? Yes,so the function is going to returnTrue.
02:54Now,let’s use thefilter() function with theis_positive function as as itsfunction argument andnumbersas its iterable argument.filteris_positive—by the way, pay attention howI’m not using the parentheses heresincefilter() needs a function object and not a function call—andnumbersas its iterable argument.
03:20Here you can see that you get afilter object, and you can’t really see theresults. But why?The reason is thatfilter() 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:48Here you go. The result is1 and2.Let’s go through what happened here.You wrote theis_positive function to take a number as an argument and returnTrue if the number is greater than0 and otherwise returnFalse. When you callfilter(),it appliesis_positive to every value innumbers,filtering out the negative numbers. This is why-2,-1, and0 have been successfully filtered out since their filteringcondition, a.k.a. theis_positive function, has evaluated asFalse.
04:22And of course,1 and2 are bigger than0, hence why they’re the answer.You just successfully extracted positive numbers of a list usingfilter(). Way togo!
04:36Now you might be wondering why would you use thisfilter() function instead ofgood oldfor loops and conditional statements? Well,because the result is shorter.
04:46Thefilter() function can be expressed in a single line of code, and the syntax ispretty intuitive. The result produced byfilter() is also faster.
04:56Why?Well,filter() is written in C and is highly optimized, andalso its internal implicit loop can be more efficient than a regularfor loopregarding execution time.
05:08This efficiency is arguably the most important advantage of using this functionin Python. The result is also more memory efficient.Thefilter() function returns an iterator,which means it only stores elements in memory as they’re needed.
05:26You learned a lot of important things in this lesson.You discoveredfilter() function, learned how to use it,and also understood its advantages over regularfor loops and conditionalstatements.
Course Contents
- Get Started With filter()
- Use filter() for Functional Programming
- Code With Pythonic Style

