Python 3.8 introduced a small but genuinely useful piece of syntax called the walrus operator, written :=. It's nicknamed "walrus" because the two characters, turned sideways, resemble a walrus's eyes and tusks. Formally it's called an assignment expression — and that name explains exactly what it does: it lets you assign a value to a variable and use that value, in the same expression, at the same time.

Normally in Python, assignment (=) is a statement, not an expression — it doesn't produce a value you can use elsewhere, which is exactly why you can't write something like if (x = get_value()): the way you might in C or JavaScript. The walrus operator exists specifically to fill that gap in situations where it genuinely improves readability.

Basic Syntax

# Without walrus: compute, then check separately
n = len("hello world")
if n > 5:
    print(f"Length is {n}, which is more than 5")

# With walrus: assign and check in a single expression
if (n := len("hello world")) > 5:
    print(f"Length is {n}, which is more than 5")

Output:

Length is 11, which is more than 5
Length is 11, which is more than 5

Both versions produce identical output, and for a case this simple, the walrus operator doesn't buy you much. Its real value shows up when the "compute, then check" pattern would otherwise force you to either repeat an expensive computation or clutter your code with a throwaway variable declared one line above where it's used.

A Practical Example: Avoiding Repeated Calls Inside a while Loop

import random

# Without walrus — the call to get a random number is awkwardly duplicated
value = random.randint(1, 10)
while value != 7:
    print(f"Got {value}, trying again...")
    value = random.randint(1, 10)
print("Found 7!")
import random

# With walrus — the assignment lives directly inside the loop condition, no duplication
while (value := random.randint(1, 10)) != 7:
    print(f"Got {value}, trying again...")
print("Found 7!")

Both snippets do the same thing, but the walrus version needs the "get a random number" logic written only once instead of twice (once before the loop, once at the end of the loop body) — a small but real improvement, since duplicated logic is a common place for bugs to creep in when one copy gets updated and the other is forgotten.

Another Common Use: Filtering Inside a List Comprehension

data = ["12", "abc", "45", "xyz", "7"]

def to_int_or_none(s):
    return int(s) if s.isdigit() else None

# Walrus lets you compute a value once, use it for both the filter and the result
results = [y for x in data if (y := to_int_or_none(x)) is not None]
print(results)

Output:

[12, 45, 7]

Without the walrus operator, this list comprehension would need to call to_int_or_none(x) twice per item — once in the filter condition, once again to produce the result — doubling unnecessary work for no benefit.

Use It Sparingly

The walrus operator is a tool for specific situations — avoiding duplicated computation in a loop condition or comprehension — not a general replacement for normal assignment. Reaching for := everywhere tends to make code harder to read, not easier, since readers aren't used to scanning for assignments buried inside a condition. When in doubt, a plain assignment on its own line, immediately followed by an ordinary if statement, is still the clearer default.