Looping around a simple list of tuples
Andrew Bennetts
andrew-pythonlist at puzzling.org
Sun Apr 27 01:02:09 EDT 2003
On Sun, Apr 27, 2003 at 04:45:16AM +0000, Martin d'Anjou wrote:
> I have a list of tuples, each tuple is a (scalar , list)
>
> a=[("r1",[1,2,3]),("r2",[8,9,10]),("r5",[12,14,16])]
>
> for el in a:
> print el
> for r,l in el:
> print "R "+`r`
> print "L "+`l`
>
> ('r1', [1, 2, 3])
> R 'r'
> L '1'
> Traceback (innermost last):
> File "<stdin>", line 3, in ?
> ValueError: unpack list of wrong size
>
> In my mind, the 'el' implicit variable is a tuple, and the for r,l loop
> should just work but it does not. How come?
I think what you want is simply:
for r,l in a:
print "R "+`r`
print "L "+`l`
Which could be spelt out as:
for el in a:
r, l = el
print "R "+`r`
print "L "+`l`
Does this make sense?
-Andrew.
More information about the Python-list
mailing list