Skip to content

Instantly share code, notes, and snippets.

@j4mie
Created February 20, 2010 02:26
Show Gist options
  • Save j4mie/309453 to your computer and use it in GitHub Desktop.
Save j4mie/309453 to your computer and use it in GitHub Desktop.
# Print all permutations formed by concatenating every word in a
# list with every other word in the same list. Good for generating
# idea/project/company names from a list of relevant words. The list
# will be sorted by word length.
from itertools import permutations
words = "spam eggs beans".split()
perms = ["".join(permutation) for permutation in permutations(words, 2)]
print "\n".join(sorted(perms, lambda a, b: len(a) - len(b)))
# Outputs:
# spameggs
# eggsspam
# spambeans
# eggsbeans
# beansspam
# beanseggs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment