[Tutor] tuple/list assignment in 'for-in'
brian will
apantomimehorse at gmail.com
Wed Apr 27 08:59:42 CEST 2005
Hi there,
I can't explain my experience with what I like to call 'parallel
assignment' (tuple/list assignment) inside 'for-in' loops and list
comprehensions:
1)
>>> [f + g for f, g in (1, 2), (3, 4)]
[3, 7]
2)
>>> x = 'ab'
>>> y = 'cd'
>>> [f + g for f, g in x, y]
['ab', 'cd']
3)
>>> f, g = '123', '456'
>>> f
'123'
>>> g
'456'
4)
>>> [f + g for f, g in '123', '456']
Traceback (most recent call last):
File "<pyshell#55>", line 1, in -toplevel-
[f + g for f, g in '123', '456']
ValueError: too many values to unpack
So why does it accept references to strings but not string literals?
The third example works with a tuple assignment of string literals but
not in the for-in? Any good reason for this, or is it just a quirk?
Thanks,
--Brian Will
More information about the Tutor
mailing list