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
2
votes
1 answer

Understanding the reason for the new python := operator

This is kind of a meta programming question: I'd like to understand why python developers introduced yet another operator with the new :=. I know what it's for. I would, however, like to know, why the developers chose a new symbol instead of…
1
vote
1 answer

Attemtping to solve twoSums leet code problem why doesn't the walrus operator with dict.get() work in python3

Prompt: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can…
0
votes
1 answer

Find fibonacci series upto N terms using list comprehension

Need to find the Fibonacci series up to N terms. n=int(input("Enter the n terms: ")) f_val=0 fib_list=[f_val:=f_val + i for i in range(n)] print(fib_list) While executing the above program, I got the result: Enter the n terms: 5 [0, 1, 3, 6,…
0
votes
3 answers

Expression that returns mutated list

I'm looking for a single expression, mutates an element and returns the modified list The following is a bit verbose # key=0; value=3; rng=[1,2] [(v if i != key else value) for i, v in enumerate(rng)] Edit: I'm looking for a way to inline the…
0
votes
2 answers

Walrus to unpack values within a list comprehension

I have a nested list holding dictionaries as mapping table using a tuple as key. I am struggling to zip the dictionary together so it can be exported by Pandas to csv file: l = [{('A', 'B'): 1}, {('A', 'C'): 2}, {('A', 'D'): 3}] def…
MafMal
  • 71
  • 5
0
votes
1 answer

Why is it that I "cannot use assignment expressions with comparison" in Python?

In the examples below x is assigned using the walrus operator and is then printed. mystring = "hello, world" #if 1 if x := mystring == "hello, world": print(x) #if 2 if x := (mystring == "hello, world"): print(x) #if 3 if (x := mystring)…
0
votes
2 answers

Why is the walrus operator not a delimiter?

The Python walrus operator (:=) is listed in the documentation as an operator, but not as a delimiter like the rest of the assignment operators (e.g. +=). Why then is the walrus operation not also a delimiter?
llll0
  • 95
  • 11
0
votes
2 answers

Use Output from expression part of list comprehension in conditional

I want to call a method and add the returned value to a list only if the value is not None, in place. Approach 1 list = [] for item in items: temp = func(item) if temp is not None: list.append(temp) Approach 2 [func(item) for item…
0
votes
2 answers

assignment expressions with conditional expression

The following snippet def expensive_function(x): return x x = 10.5 (int(y) if y.is_integer() else y := expensive_function(x)) raises SyntaxError: cannot use assignment expressions with conditional expression Can assignment expressions not be…
Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
0
votes
0 answers

Assignment expression without direct use

I'd like to introduce assignment expressions in my code where possible but I've stumbled upon a very simple case I didn't find an example for: Given a very common example [(y := f(x), x/y) for x in some_iterable] I wonder how to not use y directly,…
frans
  • 8,868
  • 11
  • 58
  • 132
-1
votes
1 answer

Why are those algorithm so different in term of their execution time ? Is the walrus operator less time consuming in Python?

While making a function to manipulate the TypeVar from the typing module, I came across a problem. My function do work but I wanted to make it faster so I used the timeit module to test some different versions. However the result aren't what I…
-1
votes
2 answers

Why does using an assignment expression in a loop condition store a bool value?

This code stores your favorite foods in a list, but the input turns into a bool type character. Why is this happening and how can I fix it? foods=list() while food := input("what food do you like?: ") != "quit": foods.append(food) print(foods)
Dani Nago
  • 13
  • 2
-1
votes
2 answers

Python3.8 assignment expressions, use in list comprehension or as an expression

I recently discovered that assignment expressions exist. And I wanted to refactor some of my code to make use of them. Most of the places I wanted to use it were relatively straightforward to convert. However, I'm not sure of the syntax to use for…
OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
-2
votes
3 answers

Using Python 3.8 assignment expressions as a let expression?

My intended behavior is: >>> x = 0 >>> with (x := 1): print(x) 1 >>> print(x) 0 However I get the expected AttributeError: __enter__ error. Is there an easy way to achieve this, or something similar that lets me compensate for not having Lisp-style…
Atonal
  • 520
  • 4
  • 14
-4
votes
0 answers

which one efficient (walrus operator)?

print('code1') a = [1, 2, 3, 4] if (len(a)) > 3: print(f"List is too long ({len(a)} elements, expected <= 3)") print('--------------------------------') print('code2') a = [1, 2, 3, 4] if (n := len(a)) > 3: print(f"List is too long ({n}…
Liam
  • 11
  • 3