Filter Iterables With Python (Overview)
Python’s filter()
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 a filtering operation. With filter()
, you can apply a filtering 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 for functional programming.
In this video course, you’ll learn how to:
- Use Python’s
filter()
in your code - Extract needed values from your iterables
- Combine
filter()
with other functional tools - Replace
filter()
with more Pythonic tools
With this knowledge, you’ll be able to use filter()
effectively in your code. Alternatively, you have the choice of using list comprehensions or generator expressions to write more Pythonic and readable code.
To better understand filter()
, it would be helpful for you to have some previous knowledge on iterables and iterators and lambda functions. You can also refresh your memory of lambda functions through a video course.
00:00 Filtering 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 of filtering iterables using Python.
00:13 But 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 that you don’t want in your final drink. To remove these unwanted elements, you decide to use a coffee filter.
00:36 After 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:57
You’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, the yield
keyword, and the next()
function. You’ll also use lambda functions a couple of times.
01:15 Remind yourself what they are and how to use them by going through How to Use Python Lambda Functions, which is both a video course and a written tutorial.
01:28 Now 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:38
After that, you’ll use Python’s filter()
function to filter numbers and strings. For example, you’ll filter odd numbers from a list of numbers and also find palindrome strings in a list of strings and filter out the non-palindrome ones.
01:54 Then you’re going to explore functional programming and pure functions.
01:58
Then you’ll move onto the map()
and reduce()
functions, and you’ll explore how you can combine them with filter()
. After that, you’ll discover the filterfalse()
function and its usage and how it promotes code reuse. And at the end, you’re going to learn how you can replace filter()
with list comprehensions and generators. Are you ready?
Become a Member to join the conversation.