#!/usr/bin/env python3 from itertools import zip_longest from itertools import repeat, chain def zip_longest_doc(*args, fillvalue=None): # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): yield counter() # yields the fillvalue, or raises IndexError fillers = repeat(fillvalue) iters = [chain(it, sentinel(), fillers) for it in args] try: for tup in zip(*iters): yield tup except IndexError: pass class Repeater: def __init__(self, o, t): self.o = o self.t = int(t) def __iter__(self): return self def __next__(self): if self.t > 0: self.t -= 1 return self.o else: raise StopIteration r1 = Repeater(1, 3) r2 = Repeater(2, 4) print('zip_longest_doc:') for i, j in zip_longest_doc(r1, r2, fillvalue=0): print(i, j) r1 = Repeater(1, 3) r2 = Repeater(2, 4) print('zip_longest_doc + list():') l = list(zip_longest_doc(r1, r2, fillvalue=0)) print(l) r1 = Repeater(1, 3) r2 = Repeater(2, 4) print('zip_longest + list():') l = list(zip_longest(r1, r2, fillvalue=0)) print(l) r1 = Repeater(1, 3) r2 = Repeater(2, 4) print('zip_longest:') for i, j in zip_longest(r1, r2, fillvalue=0): print(i, j)