Most efficient unzip - inverse of zip?

Pearu Peterson pearu at cens.ioc.ee
Tue Mar 26 15:50:14 EST 2002


Hi,

What would be the most efficient way to unzip zipped sequences 
in Python 2.2?

For example, consider

 a_b = zip(a,b)  # where a and b are some sequences
 a_b.sort()
 a,b = unzip(a_b)

For a start, I have

def unzip(seq):
  ns = range(len(seq[0]))
  r = [[] for i in ns]
  [r[i].append(s[i]) for i in ns for s in seq]
  return tuple(r)

Is there any better algorithm for unzip?

Regards,
	Pearu




More information about the Python-list mailing list