Filtering List Comprehensions
In this lesson, you’ll see that list comprehensions are often recommended by Python core developers as a more Pythonic solution than the
filter() function you used in the previous lesson.
You can also skip the middle man and use a generator expression to get the same result but not create a list in the process. A generator expressions defines an ad hoc iterator that then produces values for you without first creating a list, and then creating a tuple from that list, and then destroying the list again, so it’s more memory efficient.
All right, so in this tutorial, we mainly worked on defining this
filter() expression here—this
filter() function call—that filtered scientists by Nobel Prize, or whether or not they won the Nobel Prize.
That is what a lot of the Python core developers recommend as a replacement for the
filter() function. You can see here this is slightly different to what we had before, but we can change the formatting. All right, it’s pretty close. Now the only difference is that we’re creating a list here and previously we were creating a tuple. So, what I can do here—we’re going to create a tuple and then this is already pretty good, but we can do one better. So now, these two actually are equivalent, right?
I have the output here from this list comprehension and from the
filter() expression, they’re now actually completely equivalent, but there’s one change I can make, and that is I can get rid of the list comprehension and actually turn this piece here into a generator expression, which will have the same result, but it’s not creating a list object as an intermediary product, here. So
by doing this. I mean, this doesn’t really work, but this is kind of in the spirit of what this generator expression here did, right? You can pass something like that—like a list comprehension—you can pass it directly to the
tuple() function here or any other function and then, if you don’t have this wrapped in these square brackets, it will actually be a generator expression, which kind of defines an ad hoc iterator that then produces these values for us
02:24 without first creating a list and then creating a tuple from that list and destroying the list again. It’s more memory efficient, but you know, that’s kind of going down a rabbit hole of Python specifics here, which can be fun, but it’s not really the point of this.
02:38 I just want to make sure that I’m covering some of the sidetracks here, because I know there’s questions that come up when we’re covering concepts like that and I’m showing examples like that. All right.
So now you should have a decent idea of how the
filter() function can be used in Python, how it relates to a functional programming style, and maybe you now also have a better idea of how list comprehensions work.
So, I know the stuff we covered here at the end, it was a bit of a mouthful, and maybe it’s something you want to review a little bit slower and actually type it out. If you want to do that, I would encourage you to do it because there’s really a lot of cool things you can learn here. If you want to see the next installment in this series, the next part of this tutorial, we’re going to talk about the
map() function, and that’s another functional primitive in Python. All right! I’ll see you in the next video, and happy Pythoning!
Become a Member to join the conversation.