What does zip mean?

Grant Edwards invalid at invalid.invalid
Mon Nov 10 18:19:31 CET 2014

On 2014-11-10, giacomo boffi <giacomo_boffi at inwind.it> wrote:

> On 11/09/2014 11:44 AM, satishmlmlml at gmail.com wrote:
>> What does zip return in the following piece of code?
> To help you understanding what is the `zip` builtin, please forget
> about PKZip etc and think about the _zip fastener_ or _zipper_ in
> your bag or in your trousers
> In the bag you have two sequences of teeth that the zipper
> binds together in interlocking pairs

No, you don't. That's not how a zipper works.  Each tooth from side A,
isn't bound with one from side B.  It's bound with _two_ of them from
side B. And each of those is in turn bound with an additional tooth
from side A, and so on...

> In your program you have two lists, whose elements `zip` returns
> bound together in pairs

What the zipper on a coat does is convert two separate sequences into
a single sequence where the members alternate between the two input
sequences.  IOW if we want to do something analogous to a zipper
fastener it should do this:

  zip([a,b,c,d,e,f],[1,2,3,4,5,6])  => [a,1,b,2,c,3,d,4,e,5,f,6]
Item '1' is bound equally to item 'a' and 'b'.  Item 'b' is bound
equally to item '1' and '2'.
Grant Edwards               grant.b.edwards        Yow! I joined scientology
                                  at               at a garage sale!!

More information about the Python-list mailing list