When I use the walrus operator as below in the Python(3.9.6) interpreter, I get a syntax error: How is this different from the following? Answer It’s different because the Python core developers were very ambivalent about violating the Zen of Python guideline “There should be one– and preferably only one –obvious way to do it”, and chose to make it
Tag: walrus-operator
Python Walrus Operator in While Loops
I’m trying to understand the walrus assignment operator. Classic while loop breaks when condition is reassigned to False within the loop. Why doesn’t this work using the walrus operator? It ignores the reassignment of x producing an infinite loop. Answer You seem to be under the impression that that assignment happens once before the loop is entered, but that isn’t
Walrus operator for filtering regex searches in list comprehension
I have a Python list of strings. I want to do the regex search on each element, filtering only those elements where I managed to capture the regex group. I think I can do the regex search only once using the walrus operator from Python 3.8. So far I have: The logic is: I take the found group if the