Skip to main content
Filter by
Sorted by
Tagged with
-5 votes
1 answer
127 views

I'm doing a simple task and my "solution" raised an error. I am using the walrus operator (:=) improperly but I would like to know how I can change my code to use it properly. Code basically ...
Roko's user avatar
  • 1
3 votes
5 answers
198 views

Is there a way to abuse assignment expressions or functional tools to generate the sequence x, f(x), f(f(x)), ... in one line? Here are some contrived examples to demonstrate: def iter(x, f, lim=10): ...
qwr's user avatar
  • 11.6k
-4 votes
1 answer
124 views

Why are the negative k values being printed as the original value, but when we enter the positive realm, it gets strange? In: def j(k): for i in range(i:=k): i+=i print(i) for i in [-5,...
David M. Sousa's user avatar
-3 votes
1 answer
148 views

I'm new to programming. I'm learning the walrus operator. I want the while loop end/stop when someone type quit or finish. Only quit works. when someone type "finish", it produces an output ...
user27367938's user avatar
2 votes
1 answer
98 views

I get: return r.group() if r := re.match(rx,l) else None ^ SyntaxError: invalid syntax whereas return r.group() if (r := re.match(rx,l)) else None is accepted. What ...
ChrisJJ's user avatar
  • 2,312
1 vote
2 answers
981 views

I read the documentation and lots of examples but do not fully understand how to use it. I have a function, screen() that returns a screenshot but may return None. I call this in a loop to a variable. ...
AskedSuperior's user avatar
1 vote
2 answers
153 views

Take the following code: k = 'A' d = {} d[k := k.lower()] = 'b' print(k) print(d) This gives the output one would expect: a {'a': 'b'} However, for similar code in a project of mine, flake8 ...
Inertial Ignorance's user avatar
0 votes
1 answer
72 views

Say we have this assignment statement: a = 5 Even though it'd obviously be ugly to do this for no reason, we could technically accomplish the same thing with: (a := 5) This still assigns 5 to a, and ...
Inertial Ignorance's user avatar
-1 votes
1 answer
135 views

I have read up blogs and tutorials around the := operator but I am still trying to wrap my head around how to know when to use it. I understand its purpose is to improvement maintainability, avoiding ...
lollerskates's user avatar
  • 1,175
3 votes
1 answer
522 views

I was going to ask if there is a pylint-style code analyzer capable of suggesting the use of the := operator in places were it might improve the code. However, it looks like such test has been added ...
VPfB's user avatar
  • 18.1k
19 votes
2 answers
2k views

In Python (3.11) why does the use of an assignment expression (the "walrus operator") require wrapping in parentheses when used inside an f-string? For example: #!/usr/bin/env python from ...
Anil's user avatar
  • 1,459
0 votes
1 answer
102 views

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, ...
user12431024's user avatar
3 votes
2 answers
311 views

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?
Felix Benning's user avatar
-1 votes
1 answer
138 views

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 ...
JSaur's user avatar
  • 43
-3 votes
1 answer
2k views

I see Python examples with := and ::=. I've got a good understanding of the walrus operator from an article on RealPython(The Walrus Operator: Python 3.8 Assignment Expressions). I also went through ...
sol1000's user avatar
  • 151
0 votes
3 answers
90 views

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)] ...
Tom Huntington's user avatar
1 vote
1 answer
156 views

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 ...
Kyle Church's user avatar
0 votes
2 answers
504 views

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',...
MafMal's user avatar
  • 115
0 votes
1 answer
2k views

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 := (...
GazYah's user avatar
  • 38
0 votes
2 answers
356 views

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 ...
llll0's user avatar
  • 115
0 votes
0 answers
6k views

I just learned there is a walrus operator in Python 3.8 if a := 2 + 3: print(a) # 5 I wonder why they created a new operator instead of allowing the existing assignment operator as expression, ...
Randy Sugianto 'Yuku''s user avatar
0 votes
4 answers
971 views

I'm trying to use walrus in a for loop to create a list, something like this: data = [l := line.strip().somefunc() for line in iterable_obj if(l[0] == 'sth')] but it returns an empty list can someone ...
alitkp's user avatar
  • 11
3 votes
2 answers
1k views

I want to use the walrus operator in a dictionary declaration. However the : is propably causing a problem. I have a dictionary declaration nested in a list comprehension, but I don't want to ...
the Radek's user avatar
  • 320
0 votes
2 answers
60 views

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: ...
cc-9812398123's user avatar
2 votes
3 answers
267 views

In Python, the variable name _ (underscore) is often used for throwaway variables (variables that will never be used, hence do not need a proper name). With the walrus operator, :=, I see the need for ...
DustByte's user avatar
  • 863
1 vote
3 answers
61 views

I have a piece of code involving walrus operator. I am trying to convert it to normal python code. But I am not sure if it is happening correctly or not. # code with warlus NUM_ELEMS = cpu_count() ...
Ahmad Anis's user avatar
  • 2,742
1 vote
0 answers
78 views

I am iterating over a list of strings and matching whether the target version of last string matches current version of present string. My solution to this problem is: regex_pattern = r"^(\w+)-to-...
Asif Iqbal's user avatar
-1 votes
1 answer
580 views

This list comprehension does not work: buy_prices = [(buylow := round(0.997 + ii/10000.0, 5), max(jj, buylow)) for jj in [buylow, 0.9982] for ii in range(21)] NameError: name 'buylow' is not defined ...
EllipticalInitial's user avatar
-3 votes
3 answers
2k views

I'd like to use a walrus operator with a simple generator in a while loop def gen(): for i in range(5): yield i yield 10 g = gen() while i := next(g): print(i) I expect the ...
Harry's user avatar
  • 3,115
2 votes
1 answer
404 views

I have the following minimal working example (specific to python >= 3.8), which converts a file name string into a full path: # running this block will produce the expected output import pandas as ...
Trenton McKinney's user avatar
-1 votes
2 answers
327 views

I have a list of strings and I want to extract a pattern from elements. For instance, given list ["A 12345bcd", "BYT 676 CCC"] and pattern r'\d\d\d\d\d', I would like to obtain: [&...
M. Page's user avatar
  • 2,834
6 votes
2 answers
3k views

Is there a correct way to have two walrus operators in 1 if statement? if (three:= i%3==0) and (five:= i%5 ==0): arr.append("FizzBuzz") elif three: arr.append("Fizz") elif ...
Joseph Kready's user avatar
1 vote
3 answers
131 views

Comming from this question Adding the last position of an array to the same array I was curious if the mentioned loop can be done in a list comprehension? array = [3,4,2,5,4,5,8,7,8,9] value = 10 for ...
RSale's user avatar
  • 510
1 vote
1 answer
59 views

I am trying to use the Walrus operator in python with different if statements and the code that i am trying to replace looks like this: from time import time n = 10 numbers = [] results = [] def ...
Hamzah Zabin's user avatar
3 votes
2 answers
911 views

Playing with the walrus operator, I have this implementation of merge sort: def mergesort(array): if len(array) == 1: output = array else: pivot = len(array) // 2 left =...
Connor's user avatar
  • 4,984
-1 votes
2 answers
102 views

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?...
Dani Nago's user avatar
0 votes
1 answer
352 views

import contextlib from win32com.client import Dispatch @contextlib.contextmanager def excel_ctx() -> Generator[Dispatch, None, None] : try: yield excel := Dispatch("Excel....
Greedo's user avatar
  • 5,625
4 votes
1 answer
1k views

I was doing some python in a terminal, at a point I wrote x := 1 and it showed a Syntax Error. >>> x := 1 File "<stdin>", line 1 x := 1 ^ SyntaxError: invalid ...
Was's user avatar
  • 536
15 votes
2 answers
5k views

I wanted to avoid double evaluation of a mean in a dict comprehension, and I tried using the walrus operator: >>> dic = {"A": [45,58,75], "B": [55,82,80,92], "C":...
FAL's user avatar
  • 173
0 votes
2 answers
694 views

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 ...
OmnipotentEntity's user avatar
4 votes
1 answer
4k views

I am working on a selenium wrapper. I want to check if an element on the webpage is visible . The function gets the input variable selector which follows the pattern "selector=value" so e.g &...
Jocomol's user avatar
  • 332
2 votes
1 answer
1k views

When I use the walrus operator as below in the Python(3.9.6) interpreter, >>> walrus:=True I get a syntax error: File "<stdin>", line 1 walrus := True ...
BlueGene's user avatar
  • 1,137
8 votes
1 answer
1k views

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., ...
Mad Physicist's user avatar
1 vote
2 answers
61 views

Say we have a string like s = "a=b&c=d&xyz=abc" I would like to get dictionary {"a": "b", "c": "d", "xyz": "abc"} This ...
Roman Pavelka's user avatar
3 votes
1 answer
1k views

It would be very useful if someone could give the answer by adding a W into PEMDAS. Thanks.
Nathan Dai's user avatar
11 votes
1 answer
2k views

This is the code I write right now: a = 1 if (a := a + 1) == 2: print(a) I am wondering if something like this exists: a = 1 if (a +:= 1) == 2: print(a)
Nathan Dai's user avatar
2 votes
2 answers
2k views

I was trying to solve this problem when I thought of implementing the operator inside the return statement. Here is the question: Digital root is the recursive sum of all the digits in a number. Given ...
Hrishi's user avatar
  • 51
3 votes
1 answer
961 views

I have a Python function which returns a tuple with a boolean and string def check_something(): # ... return bool_value, str_messsage Is there a way I can use the output of this function in ...
Midhun Mathew Sunny's user avatar