Follow

Someone in (the IRC channel) just taught me that zip also unzips:

If

xy = zip(x, y)

then

x, y = zip(*xy)

(It breaks if xy is empty, so just check for that beforehand)

· · Web · 4 · 2 · 3

.... why aren't you guys as impressed as I am, c'mon, this is pretty cool.

@JordiGH I can't speak for others' lack of reaction, but in my case, it's in the official docs and example for zip anyway so I already knew about it the first time I wondered what zip did.

docs.python.org/3/library/func

@JordiGH As a related aside: the first time I wanted to know about the * argument operator, boy was that hard to search for!

@JordiGH it's not every day that you learn a new thing about a programming language that you've been using for ~20 years

@RefurioAnachro
The star unpacks a list and puts each of its elements as an argument of the function where it's used... So if xy = [a,b,c...] then f(*xy) = f(a,b,c...)
@JordiGH

Thank you, @efz1005!

That's quite a powerful operation, and unlike anything I know from type-safe languages. And I tried to guess what it does %-)

@JordiGH

@JordiGH that IS awesome! Although to see the effect in the terminal it's easier if you use list(zip(...)).

By the way, do you think that x1, ..., xn = zip(*zip(x1, ..., xn))?

Sign in to participate in the conversation
Mathstodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!