The Walrus Operator: A Hidden Gem in Python

Satish Pophale
3 min readDec 21, 2022
Generated by DALL.E 2

Python is full of little surprises and hidden gems. Just when you think you’ve learned everything there is to know, you discover something new and exciting that makes your code more efficient or easier to read.

Introduction

If you’re a Python developer, you may have come across the “walrus operator” (:=) in your code at some point. This operator was introduced in Python 3.8 and allows you to assign a value to a variable as part of an expression.

In this article, we’ll take a closer look at the walrus operator and how it can be used in your Python code. We’ll also explore some of the benefits and potential drawbacks of using this operator, and provide some examples of how it can be applied in real-world scenarios.

What is the Walrus Operator?

The walrus operator is a way to assign a value to a variable within an expression. It takes the form of variable := expression, and it works similarly to the assignment operator (=), with the exception that it can be used within an expression.

Here’s an example of how you might use the walrus operator in a while loop:

while (n := len(items)) > 0:
print(f"There are {n} items in the list")
items.pop()

In this example, the walrus operator is used to assign the value of len(items) to the variable n within the condition of the while loop. The loop will keep running as long as items has at least one element, and it will print the number of elements in items each time through the loop.

Advantages of the Walrus Operator

One of the main benefits of the walrus operator is that it can make your code more concise and easier to read. For example, consider the following code, which uses a while loop to find the first even number in a list:

i = 0
while i < len(numbers):
if numbers[i] % 2 == 0:
break
i += 1

This code works as intended, but it can be improved by using the walrus operator:

while (i := numbers.index(numbers[i])) < len(numbers):
if numbers[i] % 2 == 0:
break
i += 1

In this version of the code, the walrus operator is used to assign the value of numbers.index(numbers[i]) to the variable i within the condition of the while loop. This eliminates the need for the separate assignment statement at the beginning of the loop, and makes the code more concise and easier to read.

Drawbacks of the Walrus Operator

While the walrus operator can be a useful tool in certain situations, it’s important to be aware of its potential drawbacks as well. One potential issue is that it can make your code more difficult to read and understand, especially for developers who are not familiar with the operator.

In addition, the walrus operator is relatively new to Python, and it may not be supported by older versions of the language. This means that if you use the walrus operator in your code, it may not be compatible with systems that are running older versions of Python.

--

--

Satish Pophale

Tech-savvy Python developer, a lifelong learner and tech enthusiast with passion to solve problems and bring creative solutions to the table.