ShopperCove
Menu
All writingBlogTopicsCategoriesAboutRSS
Blog
Categories
Observability & SRE62All categories
Topics
sre111localhost lab102performance70python58javascript47bundle size36benchmarking35node.js35web development21typescript19benchmark16content creation15All topics
About
Loading article…

ShopperCove

Hands-on notes on developer tools, written after the tool has actually been run.

All writingBlogCategoriesTopicsAboutPrivacyRSS

© 2026 ShopperCove

On this page

  1. Basic example
  2. Useful patterns
  3. When not to use it
  4. Sources
  1. Blog

What Is the Walrus Operator (:=) in Python? Examples

Assignment expressions, explained with the examples that actually help.

Aditya Challa·12 October 2026·1 min read

Summary

Disclosure

This post contains affiliate links. If you buy through them, I may earn a commission at no extra cost to you. I only make firsthand claims about products I have actually used.

On this page
  1. Basic example
  2. Useful patterns
  3. When not to use it
  4. Sources

The walrus operator := assigns a value to a variable as part of a larger expression. It was added in Python 3.8 through PEP 572 and is officially called an assignment expression.

Basic example

if (n := len(items)) > 10:
    print(f"too many items: {n}")

Without it, you would call len() twice or add a separate line.

Useful patterns

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

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

When not to use it

PEP 572 and PEP 8 favour readability. Do not use := where a plain assignment statement is clearer, and note that an unparenthesised := at the top level of an expression statement is a syntax error.

Related: /blog/what-is-the-difference-between-is-and-equals-in-python

Related: /blog/are-python-dictionaries-ordered

Related: /blog/what-does-if-name-main-do-in-python

Sources

  • 01peps.python.org/pep-0572/
  • 02docs.python.org/3/whatsnew/3.8.html
pythonassignment expressionpep 572programmingsyntaxpython 3.8

Lab evidence

What I found running this

Facts in this draft were checked on 12 Oct 2026 against the primary sources listed in the body (official docs, release schedules or Google/YouTube help pages). No benchmarks or timings were run, so the post makes no performance or measurement claims; dates are quoted as published by the maintainers, and any estimate is labelled as such.

Explore the product

Freelancers and devs who want to turn coding skills into paid site builds

Build ready-made client websites faster

Check out Ready Client Sites

Notes when a lab post goes up

Occasional email for new hands-on reviews. No sequence and no sponsors.

Explore the product

Freelancers and devs who want to turn coding skills into paid site builds

Build ready-made client websites faster

Check out Ready Client Sites

Keep reading

  • What Does yield Do in Python? Generators Explained

    Generators, lazily: what yield actually does to a function.

    Summary12 Oct 20261 min read

  • How Do I Get the Current UTC Time in Python Today?

    Use a timezone-aware datetime; utcnow() is deprecated since Python 3.12.

    Summary12 Oct 20261 min read

  • How to Check if a File Exists in Python (3 Ways)?

    The modern answer is pathlib, but the right choice depends on what you do next.

    Summary12 Oct 20261 min read