Variable ReInitialisation within a oneliner?[solved] | SoloLearn: Learn to code for FREE!


Variable ReInitialisation within a oneliner?[solved]

I thought about oneliners and the only thing I cannot put in one line is the reintialisation of a variable. Example: [[[set x to 3 here],print(x)] for x in [5]] >>Output 3 So I would like to find a way how this is possible. If you can help me I would be very thankful!

8/21/2020 12:43:12 PM

Alexander Thiem

12 Answers

New Answer


You could use "the walrus operator" ( := ) instead of the regular equal sign ( = ) to assign to variables within an expression. So the code would look something like this: [[[x:=3], print(x)] for x in [5]] Visit the link below for more details:


Alexander Thiem , good to see that you could solve your 'problem'. I only wanted to mention, that the := operator is available from python 3.8, so it will not work with all versions before.


Your welcome. Glad you liked the answer!


Ali Shah Jindani Although your special code extract gave: SyntaxError: assignment expression cannot rebind comprehension iteration variable 'x' I know have a solution for my proplem! [x:=5,[print(x),[x:=3],print(x),]] makes variable reinitialisation in oneline possible. Thank you very much!


Sorry, another rant incoming. I have just read the Walrus PEP. First impression: Oh my God, what have you done to Python. Whatever happened to the Zen? Simple is better than complex? Walrus is all about creating side effects, all the while having many of these very specific rules about where you cannot use it, where you can but shouldn't use it.. Blows my mind. I get it that it can sometimes simplify a few lines.. But at what cost? It represents the exact opposite of what I value and adore in Functional Programming. No wonder Guido jumped ship after this. /rant off


pRoGRammer py Of course: If you have a code like: for i in range(int(input())): if i>10: print(i) else: print(„ „+str(i)) you can also write it in one line: [[[print(i)] if i>10 else [print(„ „+str(i))]] for i in range(int(input())] This is possible for: 1. for loops 2. function calls 3. if conditions and know appearently for 4. assignments too


Alexander Thiem do you have any specific problem in mind where reassignment seems inevitable? I haven't used the walrus all that much yet, but I am a little at odds with it. I would probably look for a more functional solution instead, involving map or reduce.


No I have no specific problem in mind. I just thought about whether every code is convertable in a oneliner and how. Variable reinitialisation was a problem, but know that is solved too. I will netherless look at map and reduce!


Tibor Santa Moreover using functional solution, you are using a different and maybe more difficult solution, instead of just formatting the normal code you wrote in a oneliner. And as C++ was and is my major programming language I have not much experience with map and reduce and so on. One more reason to look at map and reduce....


Tibor Santa I totally agree! It maked things to complex! why arent they changing it to: There is only the = operator, but it works as the walrus operator if necessary and works therefore everywhere, without limitations


As you cannot manipulate a single index of a List with the Walrus operator: x[3]=1 does not work You have to conpletely renew the List to change only a single index!


Can you explain more briefly?