0

Imagine a list of lists, like this:

x=[['foo','bar'],['baz'],['xyz']]

Except longer. I need a script to generate permutations for a list of arbitrary length containing small sets of words, also of arbitrary length.

So, in this case:

[['foo','bar'],['baz'],['xyz']]
[['foo','bar'],['xyz'],['baz']]
[['xyz'],['baz'],['foo','bar']]
[['baz'],['xyz'],['foo','bar']]
[['xyz'],['foo','bar'],['baz']]
[['baz'],['foo','bar'],['xyz']]

I've not been able to manage this with itertools. Any suggestions?

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • There is a direct function in itertools. What have you done so far? Where are you stuck? Please include your try in the question, so we can help you correct it. – Austin Oct 20 '20 at 02:29
  • Welcome to SO! `itertools.permutations(x)`! (although your ordering requirement is rather unusual/inconsistent... not sure if that matters) – ggorlen Oct 20 '20 at 02:32

1 Answers1

0

Try permutations() from itertools:

from itertools import permutations
x=[['foo','bar'],['baz'],['xyz']]
for y in permutations(x):
  print(y)
Wasif
  • 14,755
  • 3
  • 14
  • 34