The Walrus Operator
The walrus operator := is an assignment expression that assigns a value to a variable and returns that value at the same time, letting you capture a result inside a larger expression.
Learn The Walrus Operator in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Added in Python 3.8, it trims duplication in while loops, if conditions, and comprehensions — computing a value once and reusing it on the very same line.
A plain = is a statement and produces no value. The walrus := is an expression : it assigns and hands back the value, so it can live inside a bigger expression:
The walrus removes the awkward "read once before the loop, then read again at the bottom" pattern. Read and test in the condition itself:
Each loop iteration calls next(lines) , stores the result in line , and tests it — all in one place. No more forgetting to repeat the read at the bottom of the loop.
When an expensive or repeated computation appears in a comprehension's filter and its output, the walrus computes it once:
The walrus is a scalpel, not a hammer. It helps when it removes duplication; it hurts when it crams unrelated work into one line:
Ask: "does := remove duplication or save a meaningful line?" If yes, use it. If it just looks clever, prefer two ordinary lines — clarity wins.
Replace each ___ with a walrus assignment so the output matches.
✅ Use the walrus inside conditions: if (n := len(data)) > 3: . Plain = only works as its own statement.
✅ Wrap the walrus: (x := 5) . The parentheses resolve precedence and make the assignment clear.
✅ The walrus needs Python 3.8+. On older versions, assign on a separate line first.
Use the walrus to find the first running total that crosses a threshold.
Go deeper with the official Python documentation:
Lesson complete — the walrus is tamed!
You can assign and use a value in one expression with := , write cleaner while loops, reuse computations in comprehensions and conditions, and judge when it helps versus when it just looks clever.
🚀 Up next: Structural Pattern Matching — Python's powerful match/case for branching on shape, not just value.
Practice quiz
What does the walrus operator := do?
- It compares two values for equality
- It only works inside function definitions
- It assigns a value to a variable AND returns that value as an expression
- It deletes a variable after use
Answer: It assigns a value to a variable AND returns that value as an expression. The walrus := is an assignment expression: it assigns and evaluates to the assigned value, so it can live inside conditions and comprehensions.
In which Python version was the walrus operator introduced?
- Python 3.8
- Python 3.6
- Python 3.7
- Python 3.10
Answer: Python 3.8. PEP 572 added the walrus operator in Python 3.8; using it on 3.7 or older raises a SyntaxError.
Why does a plain = fail inside an if condition like 'if (n = len(data)) > 3:'?
- Because = is too slow there
- Because len() cannot be used in conditions
- Because n is a reserved word
- Because plain = is a statement, not an expression, so it produces no value to test
Answer: Because plain = is a statement, not an expression, so it produces no value to test. Plain = is a statement and yields no value, so it is a SyntaxError inside a condition. The walrus := is an expression and works.
What is the value of x after this runs: print((x := 10))?
- None
- 10
- True
- It raises an error
Answer: 10. (x := 10) assigns 10 to x and returns 10, so 10 is printed and x is now 10.