alternative to zip(*map(f, lst))?

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Tue Jan 22 14:03:17 EST 2002


Answering to my own post:

On Tue, 22 Jan 2002 18:55:27 +0000 (UTC), Huaiyu Zhu wrote:
>If f is a function that returns n-tuple, it is often useful to use
>
>a1, a2, ..., an = zip(*map(f, lst))
>
>But it breaks down when lst is an empty list.  Is there an alternative for
>doing this in one statement without for loops?  

Well, apparently not.  There is no way to know that f returns n-tuple
without actually testing it at least once.  A workaround is to explicitly
give this number

def zipmap(f, a, n=1):
	if len(a): return zip(*map(f, a))
	else: return [()]*n

wishing-python-has-some-interface-module-ly y'rs

Huaiyu




More information about the Python-list mailing list