Noob Doob Noob Doob - 1 year ago 91
Python Question

Python assign value and use on same line

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


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.

Answer Source

Here is one hack, but not pythonic. Actually since all you need is creating an object in global namespace you can simply update the namespace by your intended object.

>>> zip(globals().update(a=[1, 2, 3]) or a, a[1:])
[(1, 2), (2, 3)]

Since the update() attribute returns None, its logical or with the object would be the object itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download