Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python assign value and use on same line

Tags:

python

assign

I was trying to create a shortest-possible code for a puzzle, and the question came to mind trying to do something like this:

zip(l=[1,2,3,4,5],l[1:])

So I was wondering, is there a way to produce a value, assign it to a variable and use that variable on the very same line/function call?

EDIT:To clarify things, I am aware that this thing is not recommended nor good nor does it yield faster results. Also, the essence of the question is assignment and reusing of the same variable in the same function call. The list is produced using input, the static list here is for the example only. In a situation like this, I would like to avoid repeating the same task twice while I have already produced the result somewhere.

like image 411
Noob Doob Avatar asked Sep 02 '26 10:09

Noob Doob


1 Answers

You can use lambdas and default arguments to code golf this. Stressing that this shouldn't be used in production code, but just demonstrating what is possible in python.

(lambda l=[1, 2, 3]: zip(l, l[1:]))()
like image 198
Dunes Avatar answered Sep 05 '26 01:09

Dunes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!