Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Filters are a very useful utility in programming, letting you refine a group of items into just the ones that meet your criteria. It's a "must be this tall to ride" sign for your data!
filter()
takes a function and an iterable. The function, like with map()
, takes only a single argument and is applied to each item in the iterable. If the function returns a truthy value for the item, the item is sent back to filter()
which, ultimately, returns a new iterable of the filtered items.
You can achieve the same effect with [item for item in iterable if func(item)]
. Again, like with map()
, this can be more quickly readable for small applications.
I mentioned filterfalse()
. This function works just like filter()
but only returns things where the filter function gives back a False
or non-truthy value. You can read more in the documentation.
Watch our comprehensions workshop if you want more information.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up