unzip function?

Chris Rebert clp2 at rebertia.com
Wed Jan 18 10:38:57 EST 2012


On Wed, Jan 18, 2012 at 7:31 AM, Rodrick Brown <rodrick.brown at gmail.com> wrote:
> On Wed, Jan 18, 2012 at 10:27 AM, Alec Taylor <alec.taylor6 at 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)?

It's just the application of the
http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists
feature to a zip() [http://docs.python.org/library/functions.html#zip
] call.

zip(*zipped) === zip(*[(1, 4), (2, 5), (3, 6)]) === zip((1, 4), (2,
5), (3, 6)) === [(1, 2, 3), (4, 5, 6)]

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list