:= (walrus operator)

Added in v3.8 · Updated March 16, 2026 · Keywords
keyword assignment expression python38

The walrus operator (:=) is an assignment expression that assigns a value to a variable within an expression. Introduced in Python 3.8, it lets you assign and return a value in a single expression.

Syntax

# Basic walrus operator
(variable := expression)

# The value of the whole expression is the assigned value
result = (x := 5)  # x is 5, result is 5

How It Works

The walrus operator has two parts: the colon (:) that marks the assignment target, and the equals sign (=) that marks the value. It assigns the right-hand side value to the left-hand side name and then evaluates to that value.

# Basic usage
(x := 5)
print(x)
# 5

# The expression returns the assigned value
result = (y := 10)
print(result)
# 10

Common Use Cases

In while loops

The classic use case — process data while reading:

# Without walrus: read line, check if valid, process separately
line = file.readline()
while line:
    process(line)
    line = file.readline()

# With walrus: read and check in one expression
while (line := file.readline()):
    process(line)

List comprehensions with side effects

Avoiding repeated function calls:

import random

# With walrus: capture the value for reuse
results = [(x := random.randint(1, 10)) for _ in range(5)]
print(results)
# [3, 7, 2, 9, 5]

Conditional assignment

Assign within a condition:

# Without walrus
data = get_data()
if data:
    process(data)

# With walrus: assign and check in one step
if (data := get_data()):
    process(data)

# In elif chains
if (user := get_user(id)):
    print(f"Found: {user.name}")
elif (user := search_database(name)):
    print(f"Found in database: {user.name}")

Regular expressions

Capture match groups while checking if there was a match:

import re

text = "Price: $42.99"

# With walrus: combine in one expression  
if (match := re.search(r"(\d+)", text)):
    number = match.group(1)
    print(f"Found: {number}")

Debugging and logging

Add logging without repeating the expression:

import logging

logging.basicConfig(level=logging.DEBUG)

def expensive_operation(x):
    return x * 2

# With walrus: assign and log in one expression
logging.debug(f"Result: {(result := expensive_operation(5))}")
# DEBUG:root:Result: 10

Scope Rules

The walrus operator follows Python normal scoping rules:

# Basic scoping works as expected
(x := 5)
print(x)
# 5

# In loops, the variable persists after the loop
while (line := input("> ")) != "quit":
    print(f"You said: {line}")

# In comprehensions, the variable leaks to the enclosing scope
[x := i for i in range(3)]
print(x)
# 2 (last value assigned)

Comparison with Regular Assignment

FeatureRegular =Walrus :=
TypeStatementExpression
Can be in conditionNoYes
Returns valueNoYes
# Regular assignment is a statement - cannot be in expression

# Walrus is an expression - can be used in expressions
y = (x := 5)  # Valid
print(y)
# 5

Common Pitfalls

Forgetting parentheses

The walrus operator has low precedence. Always use parentheses:

# Without parentheses - behaves unexpectedly
x = 5
y = x := 10  # This parses as y = (x := 10)
print(x, y)
# 10 10

Reassigning in comprehensions

Comprehensions share the enclosing scope:

x = 0

# The walrus in comprehension overwrites outer x
result = [x := i for i in range(3)]
print(x)
# 2 (last value from comprehension)

See Also