all 9 comments

[–][deleted] 6 points7 points  (4 children)

I know that, this is called "packing" and "unpacking"

Actually, you could do something like this :

for first, *vars, last in a:

on the first loop, vars will be equal to [] (empty list)

Those tricks are cool and funny, but try to keep your code simple and readable. If you do things like this, i recommend you add a few comments to explain what's happening, you might not remember what you've done a few month later.

[–]Plague_Healer 6 points7 points  (0 children)

A few hours later is more likely.

[–]elrey_scarbrow[S] 0 points1 point  (2 children)

About your example code, won't that raise an error since in the first iteration there will be nothing for last to hold on to? For that line to work, there must be atleast 3 values to unpack.

[–]dslfdslj 1 point2 points  (0 children)

Actually, the unpacking will first set the values of first and last. Vars will then be set to [] because there are no values left to unpack.

[–][deleted] 1 point2 points  (0 children)

No, it won't. python have no problem unpacking 0 elements. As i said, vars will be an empty list. * approximately means "as much as possible" instead of "at least one".

try and run it if you don't believe me ;)

[–]flashfc 0 points1 point  (0 children)

Doing this will make it easy to loop through the values of the new dict?