What does this zip() code mean?

Random832 random832 at fastmail.com
Tue Sep 20 09:35:34 EDT 2016


On Tue, Sep 20, 2016, at 09:19, 380162267qq at gmail.com wrote:
> >>> x = [1, 2, 3]
> >>> y = [4, 5, 6]
> >>> zipped = zip(x, y)
> >>> list(zipped)
> [(1, 4), (2, 5), (3, 6)]
> >>> x2, y2 = zip(*zip(x, y))
> >>> x == list(x2) and y == list(y2)
> True
> 
> My problem is >>> x2, y2 = zip(*zip(x, y)).
> zip return an iterator but x2 and y2 are different?
> I really need detail explanation about this line.

Well, as you've seen, zip(x, y) is (1, 4), (2, 5), (3, 6)

This means that zip(*...) is zip((1, 4), (2, 5), (3, 6)).  It takes the
first element of each argument (1, 2, and 3), and then the next element
of each argument (4, 5, and 6).



More information about the Python-list mailing list