The Python Walrus Operator (`:=`): When to Use Assignment Expressions

Published:
Last updated:
ByJeferson Peter
2 min read
Python
Share this post:

Introduced in Python 3.8, the walrus operator (:=) generated a lot of debate.

Some developers loved it immediately. Others saw it as unnecessary syntax.

At first glance, it may look like syntactic sugar. In practice, however, the walrus operator is less about cleverness and more about removing duplication while keeping related logic close together.

This post explores when the walrus operator actually improves code and when it does not.


What Is the Walrus Operator?

The walrus operator is formally called an assignment expression.

It allows you to assign a value to a variable as part of an expression instead of as a separate statement.

Instead of:

value = compute()
if value > 10:
    ...

You can write:

if (value := compute()) > 10:
    ...

That is the core idea.


When the Walrus Operator Helps

The walrus operator is especially useful when:

  • You want to avoid repeating a function call
  • You want to keep intermediate values close to their usage
  • You are writing loops or conditionals
  • The assigned variable has a clear and limited scope

Used carefully, it makes code shorter without sacrificing clarity.


Example Without the Walrus Operator

data = input("Enter something: ")
while data != "quit":
    print(f"You typed: {data}")
    data = input("Enter something: ")

The logic is clear, but there is duplication. The input() call appears twice.


Example With the Walrus Operator

while (data := input("Enter something: ")) != "quit":
    print(f"You typed: {data}")

Now the assignment and condition are unified.

The flow becomes more compact, and the intent remains clear.


Another Practical Example

numbers = [1, 2, 3, 4, 5]

if (length := len(numbers)) > 3:
    print(f"List has {length} elements")

Here, length is computed once and immediately reused.

Without the walrus operator, you would either duplicate len(numbers) or move the assignment to a separate line.


When the Walrus Operator Hurts Readability

The walrus operator becomes problematic when it tries to do too much at once.

# Hard to read. Avoid this.
if (n := len(data)) > 10 and (avg := sum(data) / n) > 5:
    process(data)

This compresses too much logic into a single line.

Even though it works, readability suffers.

As a rule of thumb:

If the walrus operator makes a line harder to read, it probably does not belong there.


Practical Guidelines

From real-world usage, these guidelines help:

  • Use it to remove duplication
  • Avoid stacking multiple assignment expressions
  • Keep the assigned variable simple and local
  • Prefer clarity over compactness

Final Take

In my experience, the walrus operator works best when it quietly reduces repetition.

It should not feel clever. It should feel natural.

If it improves clarity and reduces duplication, use it.

If it makes the code harder to parse, skip it.

Like many Python features, the walrus operator is powerful but only when used with restraint.

Share this post: