Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leotop/8bbc0cca17efbbd3b524017a14524ffb to your computer and use it in GitHub Desktop.
Save leotop/8bbc0cca17efbbd3b524017a14524ffb to your computer and use it in GitHub Desktop.
Cartesian product of lists in Python.
def cartesian (lists):
if lists == []: return [()]
return [x + (y,) for x in cartesian(lists[:-1]) for y in lists[-1]]
print cartesian([[1, 2, 3], [2, 4, 6], [3, 6, 9]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment