Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Assignment Expression: Syntax and Practical Use

Understand when the Python assignment expression (the walrus operator) improves code and when a normal assignment is clearer, with syntax and examples.

~5 min readPython
#walrus operator#python syntax#python 3.8#conditional expressions
Illustration of the walrus operator := used in a Python conditional expression.

The assignment expression, introduced in Python 3.8, lets you assign a value to a variable as part of a larger expression. It is written with the walrus operator := and is most useful when you need to compute a value once, store it, and use it in a condition or loop. This article covers the syntax, practical patterns, common pitfalls, and the tradeoffs that determine whether using it improves your code.

The Problem That Assignment Expressions Solve

Before Python 3.8, if you needed to use a computed value both in a condition and later in the same block, you had to write the computation twice or introduce a separate assignment statement before the condition. For example, reading a line from a file until the end of the file:

python
line = f.readline() while line: process(line) line = f.readline()

This duplicates the readline() call and makes the loop logic harder to follow. An assignment expression lets you combine the assignment and the condition into a single expression, eliminating the duplication:

python
while line := f.readline(): process(line)

The walrus operator assigns the value of the right-hand side to the left-hand name and then evaluates to that same value. In the while condition, the value is used as the truthiness check, and the variable line remains available inside the loop body.

Assignment Expression Syntax and Semantics

The syntax is name := expression. When the assignment expression is not the entire surrounding expression, you usually need parentheses around the assignment itself. For example, if (n := len(a)) > 10: stores the length in n and then compares that value to 10. If you write if n := len(a) > 10:, the assignment receives the result of the comparison because the right-hand side is parsed as len(a) > 10; n becomes a boolean, not the length. This subtle difference is a frequent source of bugs.

Consider these two statements:

python
# Assigns the length of a to n, then compares n > 10 if (n := len(a)) > 10: pass # Assigns the result of len(a) > 10 (a boolean) to n if n := len(a) > 10: pass

The first form is almost always what you want when you need the length later. The second form assigns a boolean, which is rarely useful. Always parenthesize the assignment expression when its value is used in a surrounding expression such as a comparison.

Practical Use in Loops and Conditionals

The most common use is in while loops where you need to read input until a sentinel value. The file-reading example above is one case. Another is processing chunks of data:

python
while chunk := file.read(1024): process(chunk)

In if statements, assignment expressions can avoid repeated function calls or expensive computations. For example, when you need to validate a value and also use it later:

python
if (match := pattern.search(text)) is not None: process(match.group())

Without the walrus operator, you would either call pattern.search twice or assign match before the if. The former wastes work; the latter separates the assignment from the condition, making the intent less immediate.

Assignment expressions also work in list comprehensions and generator expressions, where they can store intermediate results. For example, filtering and transforming in one pass:

python
results = [y for x in data if (y := transform(x)) is not None]

This computes transform(x) once and uses it both for the filter and the output value. Without an assignment expression, you would need a loop or a nested comprehension that repeats the computation.

Common Mistakes and Reading Pitfalls

Beyond the parentheses issue, assignment expressions can hurt readability when overused. A reader scanning the code may not immediately see that a variable is being assigned inside a condition. This is especially true in complex boolean expressions:

python
if (a := f(x)) and (b := g(y)) and a > b: ...

While this works, it obscures the control flow. If the logic is not trivial, a traditional assignment before the if is often clearer.

Another mistake is using the walrus operator in a comprehension where the variable leaks into the enclosing scope. In Python 3, comprehension variables are local to the comprehension, but an assignment expression inside a comprehension assigns to the enclosing scope. This can cause unexpected side effects:

python
# In Python 3, this leaks the variable 'y' to the outer scope [x for x in range(3) if (y := x * 2)]

The variable y is now accessible after the comprehension, which may be intentional but can also lead to namespace pollution. Be aware of this behavior when using assignment expressions in comprehensions.

Performance and Readability Tradeoffs

Assignment expressions do not change the asymptotic complexity of an algorithm. Their performance benefit comes from avoiding duplicate computations. If a function is called twice with the same arguments, replacing that with a single call and a stored value reduces CPU time and memory allocations. The exact gain depends on the cost of the function and how often the code runs.

Readability is the larger tradeoff. The walrus operator can make code more concise, but conciseness is not always clarity. A developer who has not seen the pattern may pause to parse while line := f.readline():. The benefit is that it groups the assignment with the condition, which can make the loop invariant explicit. The cost is that it introduces a new syntax element that must be learned.

A reasonable rule is to use assignment expressions when they eliminate a real duplication and the resulting expression remains short and clear. Avoid them when the expression becomes deeply nested or when the variable is used far from the assignment point.

When Not to Use Assignment Expressions

There are cases where the traditional assignment statement is better. If you need to assign a value and then use it in several places, a separate assignment is clearer. For example:

python
# Clear with a normal assignment value = compute() if value > 0: use(value)

Using the walrus operator here would not reduce duplication because the value is only used once in the condition and once in the body, but the assignment is already straightforward. The operator adds no benefit.

Also avoid using assignment expressions in if conditions when the condition is already complex. The expression if (x := f(a)) and (y := g(b)) and x > y: is hard to read. A traditional approach with two assignments is more maintainable.

Finally, do not use the walrus operator to assign a value that is never used in the surrounding expression. That is simply a normal assignment in disguise and confuses readers.

Compatibility and Version Requirements

The assignment expression syntax is available only in Python 3.8 and later. If your project must support Python 3.7 or earlier, you cannot use :=. When writing code for a library or application that targets older versions, stick to traditional assignments. If you use the walrus operator in a codebase that might be run on older interpreters, the code will raise a SyntaxError at import time, not a runtime exception.

When migrating a codebase to Python 3.8+, you can gradually introduce assignment expressions in places where they clearly improve readability. Adopting a consistent style helps the team understand when the operator is appropriate.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.