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