Skip to content

Instantly share code, notes, and snippets.

@arsatiki
Created April 24, 2009 11:46
Show Gist options
  • Save arsatiki/101081 to your computer and use it in GitHub Desktop.
Save arsatiki/101081 to your computer and use it in GitHub Desktop.
def cartesian(*lists):
if not lists:
yield ()
return
for item in lists[0]:
for tup in cartesian(*lists[1:]):
yield (item,) + tup
# >>> list(cartesian('abc', [1, 2]))
# [('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment