Reverse zip() ?

Bryan Olson fakeaddress at nowhere.org
Tue Dec 2 21:16:13 EST 2008


John Machin wrote:
> Here's a version that makes it slightly easier to comprehend:
> 
> Q: I know how to zip sequences together:
> | >>> a = (1, 2, 3)
> | >>> b = (4, 5, 6)
> | >>> z = zip(a, b)
> | >>> z
> | [(1, 4), (2, 5), (3, 6)]
> but how do I reverse the process?
> 
> A: Use zip()!
> | >>> a2, b2 = zip(*z)
> | >>> a2
> | (1, 2, 3)
> | >>> b2
> | (4, 5, 6)

zip as its own inverse might be even easier to comprehend if we call zip 
by its more traditional name, "transpose".


-- 
--Bryan



More information about the Python-list mailing list