alternative to zip(*map(f, lst))?
Huaiyu Zhu
huaiyu at gauss.almadan.ibm.com
Tue Jan 22 13:55:27 EST 2002
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? It appears that list
comprehension wouldn't help here. An example of what I mean:
>>> def f(x): return x, x*3
...
>>> a, b = zip(*map(f, range(2))); print a, b
(0, 1) (0, 3)
>>> a, b = zip(*map(f, range(1))); print a, b
(0,) (0,)
>>> a, b = zip(*map(f, range(0))); print a, b
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: zip() requires at least one sequence
Is is possible to get () () in the last case? Thanks.
Huaiyu
More information about the Python-list
mailing list