I saw the code yesterday in this question , which I had not seen before, this line in particular:
for xyz[num] in possible[num]: ...
Since this loop works, elements from possible[num] assigned to the xyz list at position num . I was really confused by this, so I did some tests, and here are some equivalent codes that are a bit more explicit:
for value in possible[num]: xyz[num] = value ...
I definitely intend to always use this second format, because I find the first one more confusing than it's worth it, but I was curious ... like this:
Is there a good reason to use this "feature", and if not, why is it allowed?
Here are some silly use cases that I came up with (silly because there are much better ways to do the same), the first is to rotate the letters of the alphabet by 13 positions, and the second to create a dictionary that displays characters from rot13 to 13 characters .
>>> 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"}