Most efficient unzip - inverse of zip?

Jim Dennis jimd at vega.starshine.org
Thu Mar 28 05:03:48 EST 2002


In article <HBbo8.974$0b.153032 at news1.west.cox.net>, Tim Hochberg wrote:

>In this same vein, I like this (assuming you're using a fairly recent
>version of Python):
>>>> a = (1,2,3)
>>>> b = (4,5,6)
>>>> ab = zip(a, b)
>>>> a1, b1 = zip(*ab)
>>>> a1
>(1, 2, 3)
>>>> b1
>(4, 5, 6)

>-tim

	I'm declaring this one to be the winner!
	(Not that my opinion counts for anythin, but I'm 
	just impressed:  a1, b1 = zip(*ab)
	(or a1, b1 = apply(zip,ab)?)

	BTW: this process isn't 100% inverse.  
	It has the side effect of turning lists or strings
	(non-tuple sequences) into tuples, if they are supplied
	as the original sequences.





More information about the Python-list mailing list