[Tutor] question about for loops

taserian taserian at gmail.com
Thu Jan 7 14:46:01 CET 2010

On Thu, Jan 7, 2010 at 7:43 AM, Richard D. Moores <rdmoores at gmail.com>wrote:

> On p. 162 of "Programming In Python", 2nd ed., by Summerfield, the
> section entitled "for Loops" begins:
> =========================================
> for expression in iterable:
>     for_suite
> else:
>     else_suite
> The expression is normally either a single variable or a sequence of
> variables, usually in the form of a tuple. If a tuple or list is used
> for the expression, each item is unpacked into the expression's items.
> ======================================
> I thought I was quite familiar with for loops, but I don't understand
> how the expression can be a sequence of variables, nor what  unpacking
> into the expression's items means. Could someone explain this,
> preferably with an example?
> Thanks,
> Dick Moores
As an example, think of a list of tuples, similar to the following:

tuplelist = [ (1, "a"), (2, "b"), (3, "c") ]

To process them and handle each tuples elements individually, you can have
the for statement:

for (a, b) in tuplelist:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100107/07a63043/attachment.htm>

More information about the Tutor mailing list