Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80416 > unrolled thread

What does zip mean?

Started bysatishmlmlml@gmail.com
First post2014-11-09 02:44 -0800
Last post2014-11-14 22:06 +1100
Articles 10 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  What does zip mean? satishmlmlml@gmail.com - 2014-11-09 02:44 -0800
    Re: What does zip mean? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-09 21:58 +1100
      Re: What does zip mean? milos zorica <miloshzorica@gmail.com> - 2014-11-09 08:06 -0300
    Re: What does zip mean? giacomo boffi <giacomo_boffi@inwind.it> - 2014-11-10 17:48 +0100
      Re: What does zip mean? Grant Edwards <invalid@invalid.invalid> - 2014-11-10 17:19 +0000
        Re: What does zip mean? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-11 10:04 +1300
        Re: What does zip mean? Cameron Simpson <cs@zip.com.au> - 2014-11-11 07:36 +1100
        Re: What does zip mean? Terry Reedy <tjreedy@udel.edu> - 2014-11-10 19:39 -0500
        Re: What does zip mean? giacomo boffi <pecore@pascolo.net> - 2014-11-13 23:10 +0100
        Re: What does zip mean? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-14 22:06 +1100

#80416 — What does zip mean?

Fromsatishmlmlml@gmail.com
Date2014-11-09 02:44 -0800
SubjectWhat does zip mean?
Message-ID<baab02ae-09b4-4b9c-8e29-50e2581f9a7b@googlegroups.com>
What does zip return in the following piece of code?

curs.execute('select * from people')
colnames = [desc[0] for desc in curs.description]
rowdicts = []
for row in curs.fetchall():
           rowdicts.append(dict(zip(colnames, row)))

[toc] | [next] | [standalone]


#80417

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-09 21:58 +1100
Message-ID<545f48b9$0$13005$c3e8da3$5496439d@news.astraweb.com>
In reply to#80416
satishmlmlml@gmail.com wrote:

> What does zip return in the following piece of code?

Have you read the Fine Manual?

In Python 2, zip returns a list:

zip(['a', 'b', 'c'], [1, 2, 3])

=> [('a', 1), ('b', 2), ('c', 3)]

https://docs.python.org/2/library/functions.html#zip


In Python 3, zip does the same, except instead of a list, it returns a
special iterator which produces the items on demand rather than in advance.

https://docs.python.org/3/library/functions.html#zip



> curs.execute('select * from people')
> colnames = [desc[0] for desc in curs.description]
> rowdicts = []
> for row in curs.fetchall():
>            rowdicts.append(dict(zip(colnames, row)))

zip(colnames, row) will return:

(first column name, first item of row),
(second column name, second item of row),
(third column name, third item of row),
etc.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#80420

Frommilos zorica <miloshzorica@gmail.com>
Date2014-11-09 08:06 -0300
Message-ID<mailman.15595.1415531192.18130.python-list@python.org>
In reply to#80417

[Multipart message — attachments visible in raw view] — view raw

sorry my bad

On Sun, Nov 9, 2014 at 7:58 AM, Steven D'Aprano <
steve+comp.lang.python@pearwood.info> wrote:

> satishmlmlml@gmail.com wrote:
>
> > What does zip return in the following piece of code?
>
> Have you read the Fine Manual?
>
> In Python 2, zip returns a list:
>
> zip(['a', 'b', 'c'], [1, 2, 3])
>
> => [('a', 1), ('b', 2), ('c', 3)]
>
> https://docs.python.org/2/library/functions.html#zip
>
>
> In Python 3, zip does the same, except instead of a list, it returns a
> special iterator which produces the items on demand rather than in advance.
>
> https://docs.python.org/3/library/functions.html#zip
>
>
>
> > curs.execute('select * from people')
> > colnames = [desc[0] for desc in curs.description]
> > rowdicts = []
> > for row in curs.fetchall():
> >            rowdicts.append(dict(zip(colnames, row)))
>
> zip(colnames, row) will return:
>
> (first column name, first item of row),
> (second column name, second item of row),
> (third column name, third item of row),
> etc.
>
>
>
> --
> Steven
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Milos Zorica
+1 845 277 0549 | +1 786 471 4846

[toc] | [prev] | [next] | [standalone]


#80474

Fromgiacomo boffi <giacomo_boffi@inwind.it>
Date2014-11-10 17:48 +0100
Message-ID<m3qq8m$e0n$1@speranza.aioe.org>
In reply to#80416
On 11/09/2014 11:44 AM, satishmlmlml@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

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

[toc] | [prev] | [next] | [standalone]


#80475

FromGrant Edwards <invalid@invalid.invalid>
Date2014-11-10 17:19 +0000
Message-ID<m3qs33$n66$1@reader1.panix.com>
In reply to#80474
On 2014-11-10, giacomo boffi <giacomo_boffi@inwind.it> wrote:

> On 11/09/2014 11:44 AM, satishmlmlml@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!!
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#80491

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-11-11 10:04 +1300
Message-ID<cccnisFahtrU1@mid.individual.net>
In reply to#80475
Grant Edwards wrote:

> 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.

True, the zipper analogy isn't quite accurate. It's
hard to think of an equally concise and suggestive
name, however.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#80492

FromCameron Simpson <cs@zip.com.au>
Date2014-11-11 07:36 +1100
Message-ID<mailman.15640.1415654042.18130.python-list@python.org>
In reply to#80475
On 10Nov2014 17:19, Grant Edwards <invalid@invalid.invalid> wrote:
>On 2014-11-10, giacomo boffi <giacomo_boffi@inwind.it> wrote:
>> 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...

This is true, but the analogy is still the correct one:-)

Your nitpicking will not help the OP.

Cheers,
Cameron Simpson <cs@zip.com.au>

WFO: the normal throttle position for Denizens, squids, and unfortunates on
     50cc Honda step-throughs.

[toc] | [prev] | [next] | [standalone]


#80500

FromTerry Reedy <tjreedy@udel.edu>
Date2014-11-10 19:39 -0500
Message-ID<mailman.15645.1415666382.18130.python-list@python.org>
In reply to#80475
On 11/10/2014 3:36 PM, Cameron Simpson wrote:
> On 10Nov2014 17:19, Grant Edwards <invalid@invalid.invalid> wrote:
>> On 2014-11-10, giacomo boffi <giacomo_boffi@inwind.it> wrote:
>>> 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...
>
> This is true, but the analogy is still the correct one:-)

Perhaps ironically in this context, zippers replaced hook-and-eye 
fastening, where the two sequences *are* matched in parallel.  "Hookless 
fastener" was one of the original names (Wikipedia).

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#80705

Fromgiacomo boffi <pecore@pascolo.net>
Date2014-11-13 23:10 +0100
Message-ID<87a93uiwen.fsf@pascolo.net>
In reply to#80475
Grant Edwards <invalid@invalid.invalid> writes:

> 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'.

I love you folks of CLP

[toc] | [prev] | [next] | [standalone]


#80753

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-14 22:06 +1100
Message-ID<5465e220$0$13004$c3e8da3$5496439d@news.astraweb.com>
In reply to#80475
Grant Edwards wrote:

> 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]


While that is correct, the name "zip" for 

    zip([a,b,c],[1,2,3])  => [(a,1), (b,2), (c,3)]


is long-established. I generally call the alternate behaviour "interleaving"
or "muxing", derived from multiplexer. While muxing and demuxing is
extremely important in circuit design and telecommunications, I've never
needed it in Python programming.




-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web