The Walrus Operator (:=) in Python

Gajanan Rajput
3 min readMar 14, 2023

--

In this post we’ll have a look at Python’s walrus operator (:=), which can be used to assign and return a value in one expression. This can sometimes lead to shorter and more readable code, or save compute.

Python is a popular programming language known for its simplicity, flexibility, and ease of use. In version 3.8, Python introduced the Walrus Operator (:=), a new feature that allows you to assign values to variables within an expression. This operator is a relatively new addition to the language, and it has quickly become popular among Python developers. In this blog post, we’ll explore the Walrus Operator in Python and see how it can be used to write more concise and readable code.

👉What is the Walrus Operator (:=)?

The Walrus Operator is a new syntax introduced in Python 3.8 that allows you to assign values to variables within an expression. This is particularly useful when you want to use the value of a variable multiple times within the same expression. The Walrus Operator is denoted by the := symbol.

👉Example 1: Simple Walrus Operator Example

x = 10
if (y := x + 5) > 10:
print("y is greater than 10")
else:
print("y is less than or equal to 10")

In this example, we use the Walrus Operator to assign the value of x + 5 to y within the if statement. If y is greater than 10, we print “y is greater than 10,” and if it’s less than or equal to 10, we print “y is less than or equal to 10.”

👉Example 2: Walrus Operator in a While Loop

import random

while (n := random.randint(0, 10)) != 5:
print(n)

print("Found it!")

In this example, we use the Walrus Operator to assign a random integer between 0 and 10 to the variable n and immediately check whether it is equal to 5. The loop will continue until n is equal to 5, at which point the loop will exit and “Found it!” will be printed.

👉Example 3: Walrus Operator in List Comprehension

lst = [1, 2, 3, 4, 5]
new_lst = [x for x in lst if (x_squared := x**2) > 10]

print(new_lst)

In this example, we use the Walrus Operator in a list comprehension to create a new list containing only the elements of lst whose squared value is greater than 10. The squared value of each element is assigned to the variable x_squared within the expression.

👉Example 4: Walrus Operator with Function Return Value

def calculate_sum(lst):
if (n := len(lst)) > 0:
return sum(lst) / n
else:
return 0

lst = [1, 2, 3, 4, 5]
avg = calculate_sum(lst)

print(avg)

In this example, we use the Walrus Operator to assign the length of the list lst to the variable n within the if statement. We then return the average of the list if its length is greater than 0, and 0 otherwise.

👉Conclusion:

The Walrus Operator is a powerful new feature in Python that allows you to write more concise and readable code. It can be used in a variety of situations, such as in if statements, while loops, list comprehensions, and function return values. However, it’s important to use the Walrus Operator in a way that improves the clarity and readability of your code, rather than just for the sake of using a new feature.

Explore a world of insightful blogs at https://mrcoder701.com/. Dive into engaging content and discover more topics that resonate with your interests. Your next favorite read awaits!

Thanks for following and claps 😋

Let’s Get in Touch! Follow me on:

>GitHub: @gajanan0707

>Linkedin: Gajanan Rajput

>Website for more blogs: https://mrcoder701.com/

--

--