Skip to content

Instantly share code, notes, and snippets.

Created December 29, 2017 22:39
Show Gist options
  • Save anonymous/23d4b1862c455d0debdc8ba04a425683 to your computer and use it in GitHub Desktop.
Save anonymous/23d4b1862c455d0debdc8ba04a425683 to your computer and use it in GitHub Desktop.
from itertools import product
def lazy_product(*iterables):
iters = [[i.__iter__(), [], False] for i in iterables]
def _p_next(i, il, _):
z = i.__next__()
il.append(z)
return z
s = True
while s:
s = False
for i in (x for x in iters if not x[2]):
try:
v = _p_next(*i)
yield from product(*(j[1] if j is not i else [v] for j in iters))
s = True
except StopIteration:
i[2] = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment