Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any good use for list/dict assignment during for loop?

Tags:

python

I saw some code yesterday in this question that I had not seen before, this line in particular:

for xyz[num] in possible[num]:
    ...

So as this loop runs, the elements from possible[num] are assigned to the list xyz at position num. I was really confused by this so I did some tests, and here is some equivalent code that is a little more explicit:

for value in possible[num]:
    xyz[num] = value
    ...

I definitely intend to always use this second format because I find the first more confusing than it is worth, but I was curious... so:

Is there any good reason to use this "feature", and if not, why is it allowed?

Here are a couple of stupid use cases I came up with (stupid because there are much better ways to do the same thing), the first is for rotating the letters of the alphabet by 13 positions, and the second is for creating a dictionary that maps characters from rot13 to the character 13 positions away.

>>> import string
>>> rot13 = [None]*26
>>> for i, rot13[i%26] in enumerate(string.ascii_lowercase, 13): pass
...
>>> ''.join(rot13)
'nopqrstuvwxyzabcdefghijklm'

>>> rot13_dict = {}
>>> for k, rot13_dict[k] in zip(rot13, string.ascii_lowercase): pass
...
>>> print json.dumps(rot13_dict, sort_keys=True)
{"a": "n", "b": "o", "c": "p", "d": "q", "e": "r", "f": "s", "g": "t", "h": "u", "i": "v", "j": "w", "k": "x", "l": "y", "m": "z", "n": "a", "o": "b", "p": "c", "q": "d", "r": "e", "s": "f", "t": "g", "u": "h", "v": "i", "w": "j", "x": "k", "y": "l", "z": "m"}
like image 273
Andrew Clark Avatar asked Feb 22 '12 00:02

Andrew Clark


2 Answers

The reason this is allowed is simplicity. The list of "loop variables" has the same grammar as any other assignment target. As an example, tuple unpacking is allowed in assignments, so it is allowed in for loops as well, and this is certainly quite useful. Defining a separate syntax for the assignement of loop variables would seem artificial to me -- the semantics of regular assignment and loop variable assignments are the same.

like image 197
Sven Marnach Avatar answered Nov 03 '22 16:11

Sven Marnach


I think it's a matter of programming style wheter or not one uses this technique. Clearly it's not that obvious for outstanding people what such a loop does at first look. Therefore I myself would rather suggest using more clear methods especially if you want your code to be readable by other people that maybe don't know the language that much.

However it would be a break through the philosophy and the mechanics of python if such a construct wouldn't be allowed. This can be clearly seen, since a loop takes a sequence (a list or a string) and iterates over every element of it storing the current value in each iteration into a variable to be given. A variable is a reference to an Object and therefore it's obvious, that it doesn't matter if it's a temporary variable, a field of an array or something even different.

like image 3
s1lence Avatar answered Nov 03 '22 16:11

s1lence