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 Elements in List Comprehensions

Give Feedback

Conditional statements can be added to Python list comprehensions in order to filter out data. In this lesson, you learned how to use filtering to produce a list of even squares. The returned data is the same as before except for the fact that only even squares are returned. All the logic for this operation is done in a single line.

A list comprehension with a filter or conditional statement looks like this:

>>>
>>> even_squares = [x * x for x in range(10) if x % 2 == 0]

The next lesson will show you how to write this list comprehension using conventional for loops.

00:00 I’ve updated my example here by adding filtering, and you can see here that this looks very similar to the previous example. All of the parts that you’re already familiar with are still there.

00:09 We’ve got our output list of values, we’ve got the expression we calculate, and we’ve got this for <value> in <collection> part that we run.

00:19 This new stuff here at the end, that’s where the filtering happens. So in this case, we’re calculating the same set of values, but we’re only keeping the values where this condition here is True.

00:34 And in case you haven’t seen the modulo (%) operator before—it divides two numbers and then gives you the remainder. So, if I go 31 % 2, the remainder of that is 1, because it’s an odd number. We can’t cleanly divide it by 2, so the remainder of that is 1. And if I go 30 % 2, then the remainder is 0.

00:58 I’m taking advantage of that—here in this filtering expression—to only include values in this even_squares list that are actually even. So, this is a great way to find out if a number is even or odd.

01:11 You can just use the modulo operator for that. All right, so let’s take a look at the output here that we’ve generated. As you can see, this filtered down the previous list of squares so that it only includes the even squares. So remember, this is what we had previously, before the filtering, and that also included odd numbers—and now, those are all gone because we filtered them out with this if part in the list comprehension.

01:36 Now, let’s take a look at how that affects our template behind the scenes—or how can we transform this into a standard for loop template behind the scenes.

Zarata on April 30, 2020

You actually mean the “squares of evens”, even though the consequent squares are also even :)

Become a Member to join the conversation.