The walrus operator := is an assignment expression: it assigns a value to a name and returns that value, all inside a larger expression. Introduced in Python 3.8 by PEP 572, it lets you write if (n := len(items)) > 10: instead of computing len(items) twice or adding a separate line. This guide covers the Python walrus operator syntax, its best uses, and the places where it is not allowed.
Examples target Python 3.13 and 3.14. Anything using := needs Python 3.8 or later.
An ordinary assignment x = 5 is a statement, so it cannot appear inside an expression. The walrus operator is an expression: (x := 5) binds x to 5 and evaluates to 5.
The name comes from the shape :=, which looks like a walrus lying on its side. The main benefit is avoiding repeated work or a clumsy extra statement when you need both the value and a test on it.
Reading a file in chunks used to need a duplicated call or while True with a break.
The loop ends when read() returns empty bytes, which is falsy. The same pattern works for queue.get_nowait(), iterator.readline(), and socket reads.
Without the walrus you would assign m on one line and test it on the next. Here the match object is scoped to the same statement and reads naturally.
When a filter and the output both need an expensive result, compute it once.
Because any() short-circuits, found holds the first value that passed.
At the top level of a statement, x := 5 is a SyntaxError. Use a normal assignment there, or wrap it in parentheses. Parentheses are also required in most nested positions, which is the best habit anyway.
It is allowed without parentheses as a lone call argument (print(x := 5)) and as a direct condition in if or while (while chunk := f.read(8192):).
You cannot assign to attributes, subscripts or tuples: (obj.x := 1) and (a[0] := 1) are syntax errors. Only a simple identifier works.
A name bound with := in a comprehension belongs to the enclosing function (or module) scope, not to the comprehension. That is deliberate and lets you read it afterwards.
Related restrictions: you cannot use := in the iterable expression of a comprehension, you cannot rebind the loop variable ([i := 0 for i in range(3)] fails), and you cannot use it in a comprehension at class-body level.
A bare := inside an f-string replacement field is read as a format specifier. Parenthesise it:
The walrus operator is for cases where it removes duplication. Avoid it when a plain two-line version is clearer, and do not pack several assignments into one condition.
| Prefer this | Over this |
|---|---|
x = compute() on its own line | (x := compute()) as a statement |
One := per condition | if (a := f()) and (b := g(a)) and (c := h(b)): |
| A named variable before a comprehension | := inside a deeply nested comprehension |
PEP 8 does not forbid the walrus operator, but it asks you to use it sparingly. A good test: if a reader needs a moment to see where a name is first bound, split the line.
Python 3.8, released in October 2019, through PEP 572.
The grammar only allows an unparenthesised assignment expression in a few positions, such as the test of if or while. Elsewhere the parentheses remove ambiguity and are required.
Not meaningfully. The gain is avoiding repeated function calls or duplicated lines, not the assignment itself.
Yes, in parentheses, but the name binds inside the lambda's own scope. It is rarely a good idea.
The := operator assigns and returns a value in one expression, available since Python 3.8. Use it in while loops that read until empty, in if tests that need the matched object, and in comprehensions that reuse a computed value. Remember it needs a plain name target, parentheses in most positions, and binds in the enclosing scope inside comprehensions.