For more information on concepts covered in this lesson, you can check out:
Constructing List Comprehensions
There’s one list comprehension use case where the walrus operator can be particularly useful. Let’s say you want to apply some computationally expensive function—let’s call it
slow() in this case—to the elements in your list and filter on the resulting values.
This now only calls
slow() once per number. Unfortunately, the code is now more verbose and the intent of it is harder to understand. The list comprehension clearly signaled that you were creating a new list, while this is more concealed in the explicit
for loop since several lines of code separate the list creation and the use of
.append(). Additionally, the list comprehension runs faster than the repeated calls to
Figuring out what’s actually happening in the double list comprehension takes a fair amount of head-scratching. Essentially, the second
for statement is used only to give the name
value to the return value of
slow(num). Fortunately, that sounds like something that can be performed with an assignment expression instead.
This version is effective, readable, and communicates the intent of the code well. Note that you need to add the assignment expression on the
if clause of the list comprehension. If you try to define
value with the other call to
slow(), then it won’t work.
04:15 Let’s say you want to use the Real Python feed to find the titles of the latest episode of the Real Python Podcast. You can use the Real Python Feed Reader to download information about the latest Real Python publications.
04:30 In order to find the podcast episode titles, you’ll be using the Parse package. Start by installing them into your virtual environment. You can now read the latest titles published by Real Python.
Compiling the pattern beforehand speeds up later comparisons, especially when you want to match the same pattern over and over. You can check if a string matches your pattern using either
06:20 Let’s get back to the task at hand. In order to list all the recent podcast titles, you need to check whether each string matches your pattern and then parse out the episode title. A first attempt might look something like this.
07:29 Although it works, you might notice the same problem you saw earlier. You’re parsing each title twice because you filter out titles that match your pattern and then use that same pattern to pick out the episode title.
As you did earlier, you can avoid the double work by rewriting the list comprehension using either an explicit
for loop or a double list comprehension, but using the walrus operator is even more straightforward.
08:10 Assignment expressions work well to simplify these kinds of list comprehensions. They help you keep your code readable while you avoid doing a potentially expensive operation twice. In this section, you focused on examples where list comprehensions can be rewritten using the walrus operator. The same principles also apply if you see that you need to repeat an operation in a dictionary comprehension, a set comprehension, or a generator expression.
The generator expression uses an assignment expression to avoid calculating the length of each episode title twice. Now that you’ve seen the walrus operator in action in list comprehensions, in the next section, you’ll see it operating in a
Become a Member to join the conversation.