Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Filtering List Comprehensions

Give Feedback

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.

00:00 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.

00:11 Now, Python has this wonderful thing called list comprehensions. I could say, “All right, give me all the x’s, where x is in scientists,

00:25 but only if x.nobel is True.”

00:32 So, that is actually the Pythonic version of this filter() expression, here.

00:39 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?

01:10 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

01:37 previously, this would create—oh yeah, actually, okay. I’m going to show you that this has the same result, right? So, previously what I did, I pretty much did this: [1,2,3].

01:47 I’m creating a tuple out of a list and you can actually do this one better

01:54 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.

02:47 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.

03:01 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!

espdave4 on April 3, 2020

For line:

print(tuple(x for x in scientists if x.nobel is True))
TypeError: 'tuple' object is not callable

Any reason why?

George Yeboah on April 4, 2020

u missed the the tuple parentenses like the this print(tuple((x for x in scientists if x.nobel is True)))

Anh Lu on April 20, 2020

So if tuple(filter(lambda x: x.nobel, scientist)) and tuple(x for x in scientist if x.nobel) return the same result, when should we use one over the other?

Dan Bader RP Team on April 20, 2020

Comprehensions are generally deemed as more Pythonic, so I’d go with this version:

tuple(x for x in scientist if x.nobel)

(Technically, you’re writing a generator expression in this case but syntactically they’re the same as list comprehensions.)

quitobarajas on Aug. 5, 2020

seems a bit complicated but I’ll get the hang of it!

Become a Member to join the conversation.