I'm coming from Tcl-world ... (fwd)

Lulu of the Lotus-Eaters mertz at gnosis.cx
Sun Aug 4 01:35:10 CEST 2002


Heiko Wundram <heikowu at ceosg.de> wrote previously:
|for x in range(0,42):
|   s *= 2
|   <blah>
|   if <more blah>:
|      continue
|   <even more blah>
|Doesn't look all the more complicated, does it? Just loop over one
|variable, and specify all that extra stuff in the loop itself... :)

You can also put multiple names in the for expression itself.  What you
are actually doing if you do this is creating a tuple, but the effect is
to provide multiple loop variables.  Rewriting Wundram's example in this
style:

    for x, s in [(n, 2**n) for n in range(0,42)]:
        <do something with x and s>
        if <more blah>:
            continue
        <even more blah>

Also, if the multiple loop terms are more independent than the example,
look at the zip() builtin function (it creates a sequence of tuples out
of two or more sequences).

Yours, Lulu...

--
 mertz@  _/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: \_\_\_\_    n o
gnosis  _/_/             Postmodern Enterprises            \_\_
.cx    _/_/                                                 \_\_  d o
      _/_/_/ IN A WORLD W/O WALLS, THERE WOULD BE NO GATES \_\_\_ z e





More information about the Python-list mailing list