Filter Iterables With Python (Summary)
Python’s filter()
allows you to perform filtering operations on iterables. This kind of operation consists of applying a Boolean function to the items in an iterable and keeping only those values for which the function returns a true result. In general, you can use filter()
to process existing iterables and produce new iterables containing the values that you currently need.
In this video course, you learned how to:
- Work with Python’s
filter()
- Use
filter()
to process iterables and keep the values you need - Combine
filter()
withmap()
andreduce()
to approach different problems - Replace
filter()
with list comprehensions and generator expressions
With this new knowledge, you can now use filter()
in your code to give it a functional style. You can also switch to a more Pythonic style and replace filter()
with list comprehensions or generator expressions.
For further investigation, check out:
- 📰 Iterators and Iterables in Python: Run Efficient Iterations
- 📰 How to Use Python Lambda Functions
- 🎬 How to Use Python Lambda Functions
- 📰 How to Use Generators and
yield
in Python - 🎬 Python Generators 101
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Congratulations, you just aced this course!
00:05
In this course, you understood the concept of filtering. You used the filter()
function to keep the things you need and filter out the rest. You solved examples for both numbers and strings.
00:18
You discovered the map()
function and learned how you can use it to apply a function to each element of an iterable and also discovered the reduce()
function and learned how you can combine elements of a sequence to produce a single result.
00:33
Then you learned that by combining reduce()
, map()
, and filter()
, you can achieve complex tasks with minimal code.
00:41
You used the filterfalse()
function to keep elements that don’t meet the filtering condition, unlike the filter()
function. And you also learned how to replace filter()
with list comprehensions and generators.
00:55
If you’d like to learn more about Python iterators, lambda functions, and generator functions, check out these awesome Real Python courses and tutorials: Iterators and Iterables in Python, which is a written tutorial; How to Use Python Lambda Functions, which is both a video course and a written tutorial; and, as promised, How to Use Generators and yield
in Python, which is both a video course and a written tutorial.
01:24 This has been Negar with Real Python. Tell us what you think about this course and your favorite things that you’ve learned in the comments. Stay awesome.
Negar Vahid RP Team on July 11, 2023
@Marco Perez I’m happy that filter, map, reduce and lambdas are not scary anymore! Hope you have fun using them in your future projects. Happy Pythoning!
Become a Member to join the conversation.
Marco Perez on July 9, 2023
Very very cool. This demystified what was heretofore obscure and scary functions (filter, map, reduce) and syntax (lambdas).
Looking forward to using these lots!