5 Clever Uses of the Walrus Operator (:=) in Python You Didn’t Know!
Discover five clever ways to use the walrus operator (:=) to write cleaner and more efficient code.

Master Python’s most underrated operator!
5 Clever Uses of the Walrus Operator in Python You Didn’t Know!
Walrus operator (:=
) introduced in Python 3.8, also known as the assignment expression. It allows you to assign a value to a variable within an expression. This can make your code shorter, cleaner, and more efficient in certain scenarios.
Let’s explore 5 clever ways to use Walrus operator effectively! 🚀
Non members can read it HERE FOR FREE
1. Simplifying While Loops
The walrus operator eliminates extra variable assignments in while
loops.
If you don’t use Walrus operator you need a extra variable assignment :
data = input("Enter something: ") # First you have assign the variable
while data:
print(f"You entered: {data}")
data = input("Enter something: ")
If you use Walrus operator, it eliminates the extra variable assignment before the loop and inside the loop :
while (data := input("Enter something: ")): # It eliminates the extra variable
print(f"You entered: {data}")
2. Shorter List Comprehensions
You can store values inside a list comprehension using :=
, avoiding redundant calculations.
If you don’t use Walrus operator you need to recalculating x**2
twice :
squares = [(x, x**2) for x in range(10) if x**2 > 20]
Walrus operator avoids recalculating x**2
twice, making the code more efficient.
squares = [(x, square) for x in range(10) if (square := x**2) > 20]
3. Optimized Regular Expressions (Regex)
When using regex, the walrus operator avoids duplicate calls to re.search()
.
If you don’t use Walrus operator you need for an extra variable declaration before the if
statement :
import re
text = "My email is test@example.com"
match = re.search(r"[\w\.-]+@[\w\.-]+", text)
if match:
print(f"Found email: {match.group()}")
Walrus operator eliminates the need for an extra variable declaration before the if
statement :
import re
text = "My email is test@example.com"
if (match := re.search(r"[\w\.-]+@[\w\.-]+", text)):
print(f"Found email: {match.group()}")
4. Efficient Dictionary Updates
Instead of fetching a dictionary value multiple times, use :=
inside a condition.
If you don’t use Walrus operator you will going to fetch dictionary values multiple times :
inventory = {"apple": 5, "banana": 2}
if "apple" in inventory and inventory["apple"] > 0:
inventory["apple"] -= 1
print(f"Apples left: {inventory['apple']}")
Walrus operator reduces fetch dictionary values multiple times that will improve the code performance :
inventory = {"apple": 5, "banana": 2}
if (count := inventory.get("apple", 0)) > 0:
inventory["apple"] -= 1
print(f"Apples left: {count - 1}")
5. Condensing Function Calls in If-Statements
It Avoid redundant function calls inside if
conditions.
If you don’t use Walrus operator, you will have to call the function separately.
def fetch_data():
print("Fetching data...")
return [1, 2, 3]
data = fetch_data()
if data:
print(f"Received {len(data)} items")
The function only executes once, rather than calling it separately.
def fetch_data():
print("Fetching data...")
return [1, 2, 3]
if (data := fetch_data()):
print(f"Received {len(data)} items")
Final Thoughts
The walrus operator (:=
) is a powerful tool when used wisely! It can:
- Reduce redundant calculations
- Make loops and conditions more concise
- Improve performance in list comprehensions & function calls
Have you used :=
in your projects? Share your thoughts in the comments!