Questions tagged [python-assignment-expression]

Assignment expressions (a := b) are a new syntactic structure that were proposed in PEP 572 and introduced in Python 3.8

After acceptance of PEP 572, introduced assignment expressions, which are a way to assign to variables within an expression using the notation NAME := expr.

31 questions
252
votes
6 answers

What does colon equal (:=) in Python mean?

What does the := operand mean, more specifically for Python? Can someone explain how to read this snippet of code? node := root, cost = 0 frontier := priority queue containing node only explored := empty set
Julio
  • 2,573
  • 2
  • 11
  • 7
100
votes
6 answers

What are assignment expressions (using the "walrus" or ":=" operator)? Why was this syntax added?

Since Python 3.8, code can use the so-called "walrus" operator (:=), documented in PEP 572, for assignment expressions. This seems like a really substantial new feature, since it allows this form of assignment within comprehensions and lambdas. What…
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
30
votes
4 answers

Is it possible to add a where clause with list comprehension?

Consider the following list comprehension [ (x,f(x)) for x in iterable if f(x) ] This filters the iterable based a condition f and returns the pairs of x,f(x). The problem with this approach is f(x) is calculated twice. It would be great if we…
28
votes
1 answer

With assignment expressions in Python 3.8, why do we need to use `as` in `with`?

Now that PEP 572 has been accepted, Python 3.8 is destined to have assignment expressions, so we can use an assignment expression in with, i.e. with (f := open('file.txt')): for l in f: print(f) instead of with open('file.txt') as f: …
13
votes
1 answer

Can assignment expressions be enabled in Python 3.7 using __future__?

Python 3.8 introduces assignment expressions, described in PEP 572. Is there a way to test this new feature in Python 3.7.x? In the past, new language features have been backported to earlier Python versions using __future__ imports. Is there a…
12
votes
2 answers

Why do f-strings require brackets around assignment expressions?

In Python (3.11) why does the use of an assignment expression (the "walrus operator") require wrapping in brackets when used inside an f-string? For example: #!/usr/bin/env python from pathlib import Path import torch DEVICE =…
Anil
  • 1,097
  • 7
  • 20
7
votes
1 answer

Order of evaluation of assignment expressions (walrus operator)

I have the following expression: >>> a = 3 >>> b = 2 >>> a == (a := b) False Now, a == 2 after the operation, as expected. And the result is what I would want, i.e., comparison of a to RHS of assignment before assignment. Reversing the order of the…
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
7
votes
1 answer

How to Avoid Leaking of Python Assignment Expressions in Comprehensions

In the Effective Python book, the author recommends using assignment expressions to avoid redundancy in comprehensions, for example: def fun(i): return 2 * i result = {x: y for x in [0, 1, 2, 3] if (y := fun(x)) > 3} instead of result = {x:…
Kilian Obermeier
  • 6,678
  • 4
  • 38
  • 50
6
votes
3 answers

How to rewrite this simple loop using assignment expressions introduced in Python 3.8 alpha?

It appears to me that it is not that straight forward to interchange classic while loops with assignment-expressions-loops keeping the code looking great. Consider example1: >>> a = 0 >>> while (a := a+1) < 10: ... print(a) ...…
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
3
votes
2 answers

Equivalent of Python's Walrus operator (:=) in Julia

In python you can write if (n := len(a)) > 10: print(f"List is too long ({n} elements, expected <= 10)") is there an equivalent feature in Julia?
3
votes
3 answers

Python 3.8 assignment expression in a list comprehension

I'm trying to use the new assignment expression for the first time and could use some help. Given three lines of log outputs: sin = """Writing 93 records to /data/newstates-900.03-07_07/top100.newstates-900.03-07_07/Russia.seirdc.March6-900.12.csv…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
3
votes
1 answer

How can an assignment statement "x = y := f(x)" be done when using assignment expressions in Python?

I read in Twitter: #Python news: Guido accepted PEP 572. Python now has assignment expressions. if (match := (pattern.search) pattern.search(data)) is not None: print((match.group) mo.group(1)) filtered_data = [y for x in data if (y :=…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
2
votes
1 answer

Walrus operator in Python interpreter

When I use the walrus operator as below in the Python(3.9.6) interpreter, >>> walrus:=True I get a syntax error: File "", line 1 walrus := True ^ SyntaxError: invalid syntax How is this different from the…
2
votes
3 answers

Can assignment expression create Fibonacci series using list comprehension?

With assignment expression, I thought I could try list comprehension to create Fibonacci. I first initialize a Fibonacci list of 5 elements f = [1,2,3,4,5] with the first two values being the seeds. The test run below shows the assignment expression…
Leon Chang
  • 669
  • 8
  • 12
2
votes
1 answer

Multiple conditions for "walrus operator" assignment

I'd like to know if it's possible to use the "walrus operator" to assign the value based on some condition as well as existing. For example, assign the string to post_url if that string contains some substring: if post_url := data.get("Post url")…
Shaun Barney
  • 718
  • 10
  • 24
1
2 3