0

Possible Duplicate:
All possible permutations of a set of lists in Python

I want to find every combination of different length character lists in Python. For example, I have the following lists which define possible characters in a string:

list1=['a','d','e','f','b']
list2=['h','s','c','v','e', 'u', '?', "^", "k"]
list3=['a','d]
list4=[',','4','u','t','R','x', ' ']
list5=['%','e','u','y']

And I want to find every combination of the lists in to the string, i.e. "aha,%", "dha,%", "eha,%".

One could simply use varied nested loops, but with many more lists this is not practical.

This seems like it should be a simple problem.

Community
  • 1
  • 1
jamesmcm
  • 133
  • 1
  • 7
  • 4
    Duplicate of lots of questions, I chose one almost at random. Short version: use `itertools.product` to get all the possibilities, and then `''.join` to reconstruct the string. – DSM Sep 01 '12 at 18:39
  • 2
    It would probably be a good idea to have a list of lists, instead of that many individual lists. – Matt Sep 01 '12 at 18:47

0 Answers0