Why Use the filter() Function?
In this lesson, you’ll see why you’d want to use the
filter() function rather than, for example, a for loop with an if statement. You’d get the same result, but functional programming allows you to chain function calls. This allows you to avoid side effects and have a line of code that gives you a quick snapshot of what is happening.
When doing functional programming, you have a bunch of functions as your basic building blocks, and then you can use and reuse them together in different contexts.
00:36 When you look at this expression here, this is just calling a bunch of functions. We’re not iterating over a list, we’re not having these side effects, we’re printing stuff out. And in some ways, of course, it seems more complicated than having this simple loop here, but what’s really nice is how declarative this is, right? We’re just saying,
“Hey! Okay, we’ll take this list of scientists, apply this
filter() function, whatever it is, make sure we have a list at the end of it, and then print it out.” It’s like this long chain of function calls and that allows us to do this transformation in a single line, here.
And it’s not really about like the number of lines of code, but you know, we’re doing this with these very, very simple composable things here. We’re using the
pprint() function, we’re using the
tuple() function, we’re using this
filter() function that could be reused.
And these are all little building blocks that I could just reuse in different contexts. If you imagine I did this, let’s call this
nobel_filter(), it takes an
x, and then it returns
x.nobel is True.
having to rewrite this code or copy and pasting a bunch of codes. So, now everything we use here, it’s just a series of function applications. And it becomes clearer when I do it this way than with the
lambda, I guess, because the
lambda is kind of ugly and a lot of people in Python, they don’t really like it. And by the way, you know, before people leave angry comments, I’m not necessarily encouraging this style of programming, because it’s not really what Python was meant to do.
02:42 Arguably, this would be a better way to do it, but it can be fun to think about how you can use a functional programming style that relies on application or evaluating functions rather than writing stuff like this, and that mainly relies on immutable data structures, how that can change your mindset a little bit, and it can make things a lot easier if you’re working in parallel programming, for example, because I could apply this filter in parallel and it would be very easy to parallelize that across several threads or processes. Actually, it could be an interesting exercise.
Maybe I’ll get to that as part of these tutorials to actually show you how to do that. And if you write your code in a more functional programming style, you can take advantage of these things. All right, so I hope that gave you a good idea of how the
filter() function works. Now, I can’t really stop this part of the tutorial here without talking about list comprehensions. Or I guess, in this case, we would create a tuple.
Become a Member to join the conversation.