GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python walrus operator - using walrus operator in Python Python walrus operatorlast modified May 10, 2026 In this article we show how to use the walrus operator in Python.
Python 3.8 introduced the walrus operator, written as The walrus operator makes code more compact by combining an assignment with a condition, a return value, or a comprehension. Instead of writing the assignment on a separate line and then testing the variable, you can do both in one step. However, it should be used judiciously – clarity should never be sacrificed for brevity. print(is_new := True)
Here we assign is_new = True print(is_new) Without the walrus operator, we need two lines: one for the assignment and one for the print. The walrus version collapses them into a single expression. Reading input
In the following example, we use the walrus operator inside a
read_words.py
#!/usr/bin/python
words = []
while (word := input("Enter word: ")) != "quit":
words.append(word)
print(words)
The expression $ ./read_words.py Enter word: cloud Enter word: falcon Enter word: rock Enter word: quit ['cloud', 'falcon', 'rock'] Walrus with if condition
Suppose that all our words must have at least three characters. The walrus
operator can capture the length of a word and test it in one
test_length.py
#!/usr/bin/python
words = ['falcon', 'sky', 'ab', 'water', 'a', 'forest']
for word in words:
if ((n := len(word)) < 3):
print(f'warning, the word {word} has {n} characters')
Inside the condition $ ./test_length.py warning, the word ab has 2 characters warning, the word a has 1 characters Reading a fileThe walrus operator is especially handy when reading a file line by line. words.txt
falcon sky cloud water rock forest read_file.py
#!/usr/bin/python
with open('words.txt', 'r') as f:
while line := f.readline():
print(line.rstrip())
The Traversing a containerWhen working with lists of dictionaries that may contain missing values, the walrus operator helps to extract and test data in one go. traversing.py
#!/usr/bin/python
users = [
{'name': 'John Doe', 'occupation': 'gardener'},
{'name': None, 'occupation': 'teacher'},
{'name': 'Robert Brown', 'occupation': 'driver'},
{'name': None, 'occupation': 'driver'},
{'name': 'Marta Newt', 'occupation': 'journalist'}
]
for user in users:
if ((name := user.get('name')) is not None):
print(f'{name} is a {user.get("occupation")}')
The expression $ ./traversing.py John Doe is a gardener Robert Brown is a driver Marta Newt is a journalist
Only users with a non- Regular expressionsRegular expression searches are another natural fit. The walrus operator allows you to perform a match and check its success at the same time. search.py
#!/usr/bin/python
import re
data = 'There is a book on the table.'
pattern = re.compile(r'book')
if match := pattern.search(data):
print(f'The word {pattern.pattern} is at {match.start(), match.end()}')
else:
print(f'No {pattern.pattern} found')
$ ./search.py The word book is at (11, 15) The word book was found at indices 11 to 15. List comprehensionsOne of the most powerful uses of the walrus operator is inside list comprehensions. It lets you reuse a computed value without calling the function twice. comprehension.py
#!/usr/bin/python
def normalize(s):
return s.strip().title()
raw_names = [' john ', 'JANE', ' bob ', 'ALICE']
# Without walrus - normalization called twice
names1 = [normalize(n) for n in raw_names if len(normalize(n)) > 3]
# With walrus - computed once and captured
names2 = [name for n in raw_names if (name := normalize(n)) and len(name) > 3]
print(names1)
print(names2)
In the first comprehension
Important: Variables assigned with the walrus operator inside
a comprehension leak into the surrounding scope (in Python 3.8+). After the
comprehension runs, Function calls
The walrus operator can also be used to capture the return value of a
function call inside a retry.py
#!/usr/bin/python
import random
def unreliable_operation():
# Simulates an operation that sometimes fails
return random.choice([None, 42, None, 42])
attempts = 0
while (result := unreliable_operation()) is None and attempts < 10:
attempts += 1
print(f'Attempt {attempts} failed, retrying...')
if result is not None:
print(f'Success after {attempts} attempts, result={result}')
else:
print('All attempts failed')
Here the loop condition both calls the function and binds its return value to
Socket communicationWhen writing network code, the walrus operator can simplify loops that read data in chunks until the connection is closed. In this example we connect to a web server and receive the response piece by piece. socket_client.py
import socket
import ssl
HOST = "example.com"
PORT = 443 # HTTPS default port
# Create a default SSL context (loads trusted CA certificates)
context = ssl.create_default_context()
# Build the request
request = (
f"GET / HTTP/1.1\r\n"
f"Host: {HOST}\r\n"
f"Connection: close\r\n"
f"\r\n"
).encode("utf-8")
# Establish the connection
with socket.create_connection((HOST, PORT)) as sock:
# Wrap the raw socket with SSL
# 'server_hostname' is required for the SSL handshake (SNI)
with context.wrap_socket(sock, server_hostname=HOST) as ssock:
ssock.sendall(request)
# Use the walrus operator to read the encrypted stream
while chunk := ssock.recv(4096):
print(chunk.decode("utf-8", errors="ignore"), end="")
The call to SourceIn this article we have worked with the Python walrus operator. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |