Filter5:09 with Kenneth Love
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.
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.
You need to sign up for Treehouse in order to download course files.Sign up