Filter Iterables With Python (Overview)
Python’sfilter() is a built-in function that allows you to process an iterable and extract those items that satisfy a given condition. This process is commonly known as afiltering operation. Withfilter(), you can apply afiltering function to an iterable and produce a new iterable with the items that satisfy the condition at hand. In Python,filter() is one of the tools that you can use forfunctional programming.
In this video course, you’ll learn how to:
- Use Python’s
filter()in your code - Extractneeded values from your iterables
- Combine
filter()with otherfunctional tools - Replace
filter()with morePythonic tools
With this knowledge, you’ll be able to usefilter() effectively in your code. Alternatively, you have the choice of usinglist comprehensions orgenerator expressions to write morePythonic and readable code.
To better understandfilter(), it would be helpful for you to have some previous knowledge oniterables and iterators andlambda functions. You can also refresh your memory of lambda functions through avideo course.
00:00Filtering Iterables With Python. Welcome to this Real Python video course.I’m Negar, and I’ll be your guide as you embark on a journey to master the art offiltering iterables using Python.
00:13But what is filtering? You filter things in your life more than you think.Imagine you have a bag of freshly ground coffee beans,and you want to prepare a delicious cup of coffee. Before you start brewing,you notice that the coffee grounds contain some small particles or debris thatyou don’t want in your final drink. To remove these unwanted elements,you decide to use a coffee filter.
00:36After the coffee filter catches the unwanted debris,you’ll have your ground coffee beans ready. In other words,you kept what you needed, which were the coffee beans, and filtered out the rest,which were the unwanted elements. Throughout this course,you’ll learn how to do this, but with Python iterables.
00:57You’re going to use iterables and iterators a lot in the next lessons.To refresh your memory, try Iterators and Iterables in Python.This tutorial talks about things like generators, theyield keyword,and thenext() function. You’ll also use lambda functions a couple of times.
01:15Remind yourself what they are and how to use them by going through How to UsePython Lambda Functions, which is both a video course and a written tutorial.
01:28Now let’s talk about what you’re going to do in the next few lessons.You’ll start by understanding the filtering concept,where you’ll learn how to create filtering conditions.
01:38After that, you’ll use Python’sfilter() function to filter numbers and strings.For example, you’ll filter odd numbers from a list of numbersand also find palindrome strings in a list of strings and filter out the non-palindrome ones.
01:54Then you’re going to explore functional programming and pure functions.
01:58Then you’ll move onto themap() andreduce() functions,and you’ll explore how you can combine them withfilter(). After that,you’ll discover thefilterfalse() function and its usageand how it promotes code reuse. And at the end,you’re going to learn how you can replacefilter() with list comprehensions andgenerators. Are you ready?
Course Contents
- Get Started With filter()
- Use filter() for Functional Programming
- Code With Pythonic Style

