why does this unpacking work

Fredrik Lundh fredrik at pythonware.com
Sat Oct 21 02:30:59 EDT 2006


John Salerno wrote:

> I understand that t returns a single tuple that contains other tuples. 

t *is* a single tuple that contains other tuples.

> Then 'for x in t' returns the nested tuples themselves.
> 
> But what I don't understand is why you can use 'for x,y in t' when t 
> really only returns one thing. I see that this works, but I can't quite 
> conceptualize how.

hint:

 >>> t = (('hello', 'goodbye'),
...       ('more', 'less'),
...       ('something', 'nothing'),
...       ('good', 'bad'))
 >>> t[0]
('hello', 'goodbye')
 >>> t[1]
('more', 'less')
 >>> t[2]
('something', 'nothing')
 >>> t[3]
('good', 'bad')

</F>




More information about the Python-list mailing list