# ๐Ÿ” Understanding `zip(*zip(x, y))` in Python

Yeeunยท2025๋…„ 4์›” 16์ผ

Python

๋ชฉ๋ก ๋ณด๊ธฐ
7/31

๐Ÿ” Understanding zip(*zip(x, y)) in Python

How zip can both pack and unpack like magic!


๐Ÿงฉ What's zip in Python?

The built-in zip() function pairs up elements from multiple iterables.

x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
print(list(zipped))  # [(1, 4), (2, 5), (3, 6)]

Now hereโ€™s something interesting:

x2, y2 = zip(*zip(x, y))
print(x2)  # (1, 2, 3)
print(y2)  # (4, 5, 6)

๐Ÿ” Whatโ€™s happening here?

Letโ€™s break it down.

๐Ÿ”น zip(x, y)

Combines the elements pair-wise:

[(1, 4), (2, 5), (3, 6)]

๐Ÿ”น *zip(x, y) โ†’ Unpacking

The asterisk * unpacks the list of pairs into separate arguments:

zip((1, 4), (2, 5), (3, 6))

So it's like we're zipping the columns of the original pair list.


๐Ÿ’ก Why is it useful?

It transposes the data โ€” rows become columns:

zip(*[(1, 4), (2, 5), (3, 6)]) 
โ†’ zip((1, 4), (2, 5), (3, 6)) 
โ†’ [(1, 2, 3), (4, 5, 6)]

You get:

x2 = (1, 2, 3)
y2 = (4, 5, 6)

So this line:

x2, y2 = zip(*zip(x, y))

is a way to unzip or reverse the original zip operation.


โœ… Final check

x == list(x2) and y == list(y2)  # True

Everything matches โ€” zip/unzip is successful!


๐Ÿ“– Official Python Docs

You can find this trick (and more like it) in the official Python zip() documentation here:

๐Ÿ”— https://docs.python.org/3/library/functions.html#zip

It even includes this line:

โ€œAn idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).โ€


๐Ÿง  TL;DR

ExpressionMeaning
zip(x, y)Combine x and y into pairs
zip(*zip(x, y))Reverse the zip โ€” unzip it!
* operatorUnpacks zipped list into arguments

This is one of those Pythonic idioms that feels a bit magical โ€” but now you know the logic behind it ๐Ÿ”ฅ

0๊ฐœ์˜ ๋Œ“๊ธ€