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.

How to Supercharge Your Comprehensions

00:00 In order to understand the full value that list comprehensions can provide, it’s helpful to understand their range of possible functionality. You’ll also want to understand the changes that were made to list comprehension in Python 3.8, so we’ll cover these also. Firstly, let’s look at using conditional logic. In the previous lesson, you saw this formula for how to create list comprehensions. While this formula is accurate, it’s also a bit incomplete.

00:28 A more complete description of the comprehension formula adds support for optional conditionals. The most common way to add conditional logic to a list comprehension is to add a conditional to the end of the expression. Here, your conditional statement comes just before the closing bracket.

00:49 Conditionals are important because they allow list comprehensions to filter out unwanted values, which would normally require a call to the filter() function.

00:58 Let’s have a look at an example. In this code block, the conditional statement filters out any characters in sentence that aren’t a vowel. The conditional can test any valid expression.

01:11 If you need a more complex filter, then you can even move the conditional logic to a separate function. Here you create a complex filter called is_consonant() and pass this function as the conditional statement for your list comprehension.

01:26 Note that the member value i is also passed as an argument to your function. You can place the conditional at the end of the statement for simple filtering.

01:38 But what if you want to change your member value instead of filtering it out? In this case, it’s useful to place the conditional at the beginning of the expression.

01:48 With this formula, you can use conditional logic to select from multiple possible output options.

01:56 For example, if you have a list of prices, then you may want to replace negative prices with 0 and leave the positive values unchanged. Here, your expression i contains a conditional statement, if i > 0 else 0.

02:12 This tells Python to output the value of i if the number is positive, but to change i to 0 if the number is negative.

02:21 If this seems overwhelming, then it may be helpful to view the conditional logic as its own function.

02:28 Now your conditional statement is contained within the get_price() function, and you can use it as part of your list comprehension expression.

02:39 While the list comprehension in Python is a common tool, you can also create sets and dictionary comprehensions. A set comprehension is almost exactly the same as a list comprehension in Python.

02:51 The difference is that set comprehensions make sure that the output contains no duplicates. You can create a set comprehension by using curly braces instead of brackets.

03:03 Your set comprehension outputs all the unique vowels it found in quote. Unlike lists, sets don’t guarantee that items will be saved in any particular order.

03:15 Dictionary comprehensions are similar, with the additional requirement of defining a key. To create the squares dictionary you use curly braces as well as a key-value pair, i: i * i in your expression.

03:34 Python 3.8 introduced the assignment expression, also known as the walrus operator. It allows you to run an expression while simultaneously assigning the output value to a variable. To understand how you can use it, let’s have a look at an example.

03:51 Say that we need to make 10 requests to an API that will return temperature data. You only want to return results that are greater than a hundred degrees Fahrenheit. Assume that each request will return different data. In this case, there’s no way to use a list comprehension in Python to solve the problem.

04:08 The formula expression for <member> in <iterable> if <conditional> provides no way for the conditional to assign data to a variable that the expression can access. The walrus operator solves this problem.

04:21 As I already mentioned, it allows you to run an expression while simultaneously assigning the output value to a variable. Let’s have a look at this in an example.

04:31 In this case, we’re going to use the get_weather_data() function to generate fake weather data. You won’t often need to use the assignment expression inside of a list comprehension in Python, but it’s a useful tool to have at your disposal when necessary.

04:47 So now you know how to supercharge your list comprehensions and how useful they can be, but it’s also important to understand that there are certain circumstances in which they are not the right choice. In the next lesson, you’ll learn when not to use a list comprehension in Python.

Become a Member to join the conversation.