Other resources: Python’s filter() function tutorial
Filtering Iterables
00:00
In the previous lesson I showed you how to use the built-in map function. In this lesson, I’ll be covering the built-in filter.
00:08
The arguments to filter are similar to map. It takes a callable and an iterable. Like with map, it also invokes the callable on each item in the iterable.
00:18
This time though, it treats the return from the callable as a truthy value, and the result from filter only contains those items that evaluated to true.
00:28
It’s roughly equivalent to this code. Loop through the iterables. If the callable invoked with an item is truthy, then include it in the output. filter does exactly what its name implies. It filters things from an iterable.
00:42 Let’s go see this in action in the REPL. First, I’ll need some data to filter.
00:52 If I wanted to see each value that was bigger than 100,
01:04
I could evaluate each item and print it out, or I could use filter.
01:19
Here, I’ve used a lambda as my evaluator callable, passing it and the values iterable to filter. filter invokes the lambda on each item, and only those values larger than 100 will result in True.
01:33
filter returns an iterable itself, which gets used here in the for loop. This is the same pattern as the first map I showed you, just this time instead of mapping values, it’s filtering them out.
01:48
And like with map, filter returns an object,
01:57
so more of that déjà vu stuff. filter and map aren’t the only Python functions that behave this way. You’ve probably used range with a for loop, but have you ever looked at it on its own?
02:09
Yep. It returns an iterable range object, which when evaluated in the REPL shows the start and stop points of that range. If I want to see what’s inside of it, I convert it to a list.
02:23
Let’s combine this with filter to get even numbers.
02:34
Nice little one-liner there. Granted, there’s an easier way as range accepts a step argument, but this isn’t a course on range.
02:42 Let me set up some more data here.
02:50
Like everything else in Python, strings are objects. They even have methods. The .isupper() method on a string returns True if the string is all uppercase.
03:02 Quick little historical aside, do you know why they’re called upper and lowercase? Their proper names are actually majuscule and minuscule. In the original days of the printing press, each piece of movable type was stored in a tray or case.
03:16
The majuscule type was stored in the upper case, while the minuscule was stored in the lower one, and the term stuck. It’s a good thing too, as I’d probably need to dig out a dictionary if I had to type “is majuscule” each time I wanted to invoke the method. The first piece of fruit here returned False as apples is all minuscule, whereas the last piece of fruit is True since it’s all majuscule.
03:43 Let’s use this idea to filter our list of fruit.
03:53 A quick little lambda, and the result is an iterable of the fruit that are all uppercase. You may have caught the fact that I’ve been a bit more vague than just true and false.
04:04 In Python, you can do Boolean evaluation on more than just Boolean types.
04:13 Consider this list of stuff.
04:20
A quick filter shows just what Python thinks is truthy: any non-empty string, actually, any non-empty sequence, dictionary, or set, and any non-zero integer are considered true.
04:33
Zero, false, empty strings, and None are considered false. Thus, my list here gets filtered down to just 'a' and 1.
04:44
Next up, the last of the three common functional programming tools, the reduce function.
Become a Member to join the conversation.
