Skip to content

Instantly share code, notes, and snippets.

@ahirschberg
Created March 29, 2015 02:06
Show Gist options
  • Select an option

  • Save ahirschberg/dd4449447aa274b002dd to your computer and use it in GitHub Desktop.

Select an option

Save ahirschberg/dd4449447aa274b002dd to your computer and use it in GitHub Desktop.
Sam's all permutations of a list in python
# Sam's python implementation
def permutations(listt): # apparently 'list' is a reserved keyword in python (or at least is a different color in the editor)
if len(listt) == 1:
yield listt
return
else:
for i, o in enumerate(listt):
listt_copy = listt.copy()
del listt_copy[i]
for perm in permutations(listt_copy):
yield [o] + perm # still not quite clear on how the yield statements work here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment