Andrew Dalke wrote: > 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.
Seems my description didn't convince you. So here's an example: >>> 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) ... >>> list(fillzip(range(6), range(3))) [(0, 0), (1, 1), (2, 2)] >>> versus >>> map(None, range(6), range(3)) [(0, 0), (1, 1), (2, 2), (3, None), (4, None), (5, None)] Now where's the typo? > 'Course then the name should be changed. My variable names where ill-chosen to begin with. Peter -- http://mail.python.org/mailman/listinfo/python-list