The world’s leading publication for data science, AI, and ML professionals.

The Walrus Operator in Python

Learn what the walrus operator is and how to use it in Python

Photo by Jay Ruzesky on Unsplash
Photo by Jay Ruzesky on Unsplash

The walrus operator, introduced in Python 3.8, offers a way to accomplish two tasks at once: assigning a value to a variable, and returning that value, which can sometimes offer a way to write shorter, more readable code, that may even be more computationally efficient.

Let’s review what the walrus operator is and some examples of when it can be useful.


simple assignment operator

We are all familiar with how to assign a value to a variable. We do so using the simple assignment operator:

num = 15

And if we wanted to print the value of this variable using the print function, we can pass in the variable num as follows:

print(num)
# 15

enter the walrus operator

Introduced in python 3.8, the walrus operator, (:=), formally known as the assignment expression operator, offers a way to assign to variables within an expression, including variables that do not exist yet. As seen above, with the simple assignment operator (=), we assigned num = 15 in the context of a stand-alone statement.

An expression evaluates to a value. A statement does something.

In other words, the walrus operator allows us to both assign a value to a variable, and to return that value, all in the same expression. The name is due to its similarity to the eyes and tusks of a Walrus on its side.

name := expr

expr is evaluated and then assigned to the variable name. That value will also be returned.

Let’s look at some examples of the walrus operator in use.


simple example

The best way to understand the walrus operator is with a simple example. Just like above, we want to assign 15 to num, and then print the value of num. We can accomplish both these tasks in one line of code using the walrus operator as follows:

print(num := 15)
# 15

The value of 15 is assigned to num. Then this same value is returned, which will become the argument for the print function. Thus, 15 is printed.

If we attempt the above using the simple assignment operator, we would get a TypeError, since num = 15 does not return anything.

print(num = 15)
# TypeError

Three Concepts to Become a Better Python Programmer


another example

Let’s say that we want to keep asking a user for some input. If the user does not enter anything, then we want to stop asking for more inputs. We can do this using a while loop as follows:

We ask the user to enter something, and assign that input to value. We then create a while loop that runs if the value entered is not an empty string. We print out ‘Nice!’ if the user successfully entered something. We then ask the user for another input and set that equal to value, and restart the process.


Let’s now try this using the walrus operator:

We ask the user for an input, and set that input equal to value using the walrus operator. This value is also returned, and compared to an empty string. If that comparison evaluates to True (not equal to an empty string), the code in the while loop runs, and ‘Nice!’ is printed. If it evaluates to False, the code does not run.


example with list comprehension

An example where the walrus operator can improve code readability and its computational efficiency is within list comprehensions that aim to filter out values.


List Comprehensions in Python


For example, let’s say we have the list of numbers _num_list_, and we want to add the cube of those numbers only if the cubed value is less than 20. We can do so as follows:

Notice how we have to call the function cube twice.

The walrus operator will allow us to only call the function cube once in our list comprehension, as follows:

The value of cube(x) is assigned to y, then returned and compared to 20. The value of y will only be added to the list if it is less than 20. Notice how the cube() function was only called once, leading to more efficient code. This efficiency improvement can become more substantial the more complex and computationally demanding the function is.


I hope this tutorial on how to use the walrus operator in Python was helpful. Thank you for reading!


Related Articles