It seems your output is limited by the output of the first it1 iterator. Thus, we could use it1 as is, and pad it2 with an infinite None reinforcing iterator and zip them.
>>> from itertools import repeat,izip,chain >>> somezip = lambda it1,it2: izip(it1,chain(it2,repeat(None))) >>> list(somezip(a,b)) [(1, 5), (2, 6), (3, 7), (4, 8)] >>> list(somezip(a,c)) [(1, 10), (2, 11), (3, 12), (4, None)]
repeat(None) creates an iterator inferior to None infinitely.
chain gluing it2 and repeat(None) .
izip will stop working as soon as it1 is exhausted.
Other solutions have some drawbacks (I left comments in the comments). They may work well, but with some input they may unexpectedly fail.
As the glglgl suggested in the comments, this function is more likely to accept a variable number of iterators in the parameters.
So, I updated the code to work:
from itertools import repeat,izip,chain,imap somezip = lambda it1,*its: izip(it1,*imap(chain,its,repeat(repeat(None))))
Test:
>>> print(list(somezip(a,b))) print(list(somezip(a,c))) print(list(somezip(b,a,c))) [(1, 5), (2, 6), (3, 7), (4, 8)] [(1, 10), (2, 11), (3, 12), (4, None)] [(5, 1, 10), (6, 2, 11), (7, 3, 12), (8, 4, None), (9, None, None)]
I had to use imap here, although there was no need to do this (since the parameter will be unpacked later, the usual map will be executed). The reason was that map did not accept iterators of different lengths , and imap stopped while consuming the smallest iterator.
So imap applies chain to all iterators except the first one, and chain each of them with repeat(None) . To serve each of its iterators, I used another repeat above repeat(None) (note that this can be very dangerous in other projects, since all objects that the external repeat produces are the same repeat(None) object, therefore, after all, all the teters of it chain share it). Then I unpacked the imap object to get the izip parameters, which returns values until it1 is consumed (since chain ed its now produce an infinite sequence of values each).
Please note that all operations are performed in pure C, so there is no need to use the interpreter overhead.
To clarify how this works, I add this development:
def somezip(it1,*its):
And one-line for him is simple:
somezip = lambda it1,*its: izip(it1,*imap(chain,its,repeat(repeat(None))))