map, filter, lambda, list comprehensions (was RE: parameterundefined in procedure)
Tim Peters
tim_one at email.msn.com
Sun Feb 27 16:08:17 EST 2000
[Greg Ewing's original list comprehension example, from '98]
> def zip(L1, L2):
> return [(x, y) for x in L1, y in L2]
[Michal Wallace]
> is this the same as:
> [code that acts much like Python's current map(None, L1, L2), except
> builds a list of two-lists instead of two-tuples]
Close. It was long ago agreed that map's None-padding of "short" sequences
is at best a dubious idea, and to drop that for (the not yet implemented)
"parallel iteration" (whether in "for" or list comprehension context). So
it would actually act like:
def zip(L1, L2):
result = []
i = 0
while 1:
try:
next = L1[i], L2[i]
except IndexError:
break
result.append(next)
i = i+1 # ignoring a subtlety here on OverflowError
return result
Note that Python's iteration protocol terminates via exception, not via
reaching a precomputed length. This is so you can iterator over, e.g.,
lines of a file, where you don't know len() in advance.
More information about the Python-list
mailing list