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


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

Re: unzip function?

Started byBenjamin Kaplan <benjamin.kaplan@case.edu>
First post2012-01-18 10:37 -0500
Last post2012-01-18 10:37 -0500
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: unzip function? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-01-18 10:37 -0500

#19087 — Re: unzip function?

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2012-01-18 10:37 -0500
SubjectRe: unzip function?
Message-ID<mailman.4833.1326901103.27778.python-list@python.org>
On Wed, Jan 18, 2012 at 10:31 AM, Rodrick Brown <rodrick.brown@gmail.com> wrote:
>
>
> On Wed, Jan 18, 2012 at 10:27 AM, Alec Taylor <alec.taylor6@gmail.com>
> wrote:
>>
>> http://docs.python.org/library/functions.html
>> >>> x = [1, 2, 3]
>> >>> y = [4, 5, 6]
>> >>> zipped = zip(x, y)
>> >>> zipped
>> [(1, 4), (2, 5), (3, 6)]
>> >>> x2, y2 = zip(*zipped)
>> >>> x == list(x2) and y == list(y2)
>> True
>
>
> Alec can you explain this behavior zip(*zipped)?

Zip is its own inverse.
>>> zip([1,2,3],[4,5,6])
[(1, 4), (2, 5), (3, 6)]
>>> zip((1,4),(2,5),(3,6))
[(1, 2, 3), (4, 5, 6)]

If you're not sure of the *zipped part, it just means treat each
element of zipped as a different argument to zip. So zip(*zipped) is
calling zip(zipped[0], zipped[1]... zipped[-1])

[toc] | [standalone]


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


csiph-web