Skip to content

Instantly share code, notes, and snippets.

@rioleo
Created November 16, 2011 16:36
Show Gist options
  • Save rioleo/1370583 to your computer and use it in GitHub Desktop.
Save rioleo/1370583 to your computer and use it in GitHub Desktop.
lexicographic-permutations
def permutations(li):
""" Return all permutations of a given list. This function
assumes every element of the list is unique. """
if len(li) <= 1:
yield li
else:
for el in li:
for p in permutations([e for e in li if e != el]):
yield [el] + p
x = permutations([1, 2, 3, 4, 5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment