3 ms·
In python the inverse of the zip function is zip. You call it with with * to make list elements into function arguments. So a, b = zip(*c) is the inverse of c =
by fouronnes3 4y ago
In python the inverse of the zip function is zip. You call it with with * to make list elements into function arguments. So a, b = zip(*c) is the inverse of c = zip(a, b).
- deleted 4y ago[deleted]
- tmh88j 4y agoI think it only works out that way when you're unpacking the same number of variables as there are elements in c, which is the same as (a,b) = c and c = (a,b). If you tried something like c = (('d','e','f'),('g','h')) and then a,b = zip(*c) you'd get a = ('d','g') and b = ('e','h'). The 'f' that was originally at c[0][2] is dropped so you can't zip back to the original value of c, unless that's how inverses work for this scenario and my math is rusty.
- PartiallyTyped 4y agoIt works only because zip drops extra items. I explained how it works in the reply below.
- PartiallyTyped 4y agoThe expansion of c is a tuple of pairs, zip then creates an iterator (iirc zips get exhausted so iterator and not iterable); the unpack then calls dunder iter of the iterator. The iterator returns itself; and the destructing expands the iterator to exactly two items and binds them to a,b
- PartiallyTyped 4y agodestructuring, not destructing.