[snip too-long subject]

Garry Knight garryknight at gmx.net
Sun Mar 21 01:07:50 CET 2004


In message <dVGXAls/KHBK089yn at the-wire.com>, Mel Wilson wrote:

> In article <7x7jxhuuex.fsf at ruckus.brouhaha.com>,
> Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:
>>Preferred style these days is to use list comprehensions instead of
>>map and zip.
> 
> zip?  Do you mean filter?

>From 'Learning Python' by Mark Lutz and David Ascher:

<quote>

The built-in zip function allows us to use for loops to visit multiple
sequences in _parallel_. In basic operation, zip takes one or more
sequences, and returns a list of tuples that pair up parallel items taken
from its arguments. For example, suppose we're working with two lists:

>>> L1 = [1,2,3,4]
>>> L2 = [5,6,7,8]

To combine the items in these lists, we can use zip:

>>> zip(L1,L2)
[(1, 5), (2, 6), (3, 7), (4, 8)]

Such a result may be useful in other contexts. When wedded with the for
loop, though, it supports parallel iterations:

>>> for (x,y) in zip(L1, L2):
...     print x, y, '--', x+y
...
1 5 -- 6
2 6 -- 8
3 7 -- 10
4 8 -- 12

</quote>

It might be useful to think of the items in each list coming together like
the teeth of a zipper. But it's better than that: you're not constrained to
two sets of teeth:

>>> L3 = [9, 10, 11, 12]
>>> zip(L1,L2,L3)
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

-- 
Garry Knight
garryknight at gmx.net  ICQ 126351135
Linux registered user 182025



More information about the Python-list mailing list