can list comprehensions replace map?

Andrew Dalke dalke at dalkescientific.com
Fri Jul 29 20:02:20 CEST 2005


Me:
>> Could make it one line shorter with
>  
>> from itertools import chain, izip, repeat
>> def fillzip(*seqs):
>>     def done_iter(done=[len(seqs)]):
>>         done[0] -= 1
>>         if not done[0]:
>>             return []
>>         return repeat(None)
>>     seqs = [chain(seq, done_iter()) for seq in seqs]
>>     return izip(*seqs)

Peter Otten:
> that won't work because done_iter() is now no longer a generator.
> In effect you just say
> 
> seqs = [chain(seq, repeat(None)) for seq in seqs[:-1]] + [chain(seq[-1],
> [])]

It does work - I tested it.  The trick is that izip takes iter()
of the terms passed into it.  iter([]) -> an empty iterator and
iter(repeat(None)) -> the repeat(None) itself.

'Course then the name should be changed.

				Andrew
				dalke at dalkescientific.com




More information about the Python-list mailing list