Not sure how to look around for this, but from itertools the function izip_longest does this:
izip_longest('ABCD', 'xy', fillvalue='-') –> Ax By C- D-
I was hoping an iterable library would have something to do this:
izip_longest_better('ABCDE', 'xy') –> Ax By Cx Dy Ex
Preferably for an arbitrary number of iterables, being used to generate millions of combinations. I’ll write my own, but I figured I’d ask, since I’m sure my own won’t be very pythonic.
Awesome, It was the cycle that I hadn’t tried. I was also able to get something working by nesting for loops on arrays instead of iterators, but this is much better. What I finally used was this to handle similar to izip”
EDIT:
Ended up with
def izip_longest_repeat(*args):
if args:
lists = sorted(args, key=len, reverse=True)
result = list(itertools.izip(*([lists[0]] + [itertools.cycle(l) for l in lists[1:]])))
else:
result = [()]
return result
Something like this?
etc. And there’s the arbitrary-number-of-iterables variant (assuming that you don’t want the first argument to cycle, and that you’re not really interested in itertools.product):