0

I need to generate ALL possible combinations of EACH element in each list of strings

list1 = ['The girl', 'The boy']
list2 = ['wears', 'touches', 'tries']
list3 = ['a red sweater', 'a blue sweater', 'a yellow sweater', 'a white sweater']

So the result is a list of a string combining each element combines with each other element:

The girl wears a red sweater
The boy wears a red sweater
The girl touches a red sweater
The boy touches a red sweater
The girl wears a blue sweater
The boy wears a yellow sweater
(ETC...)

I don't particularly care about the order of the output as long as ALL combinations are obtained.

From my research I guess "permutation" would be a solution but I only found several answers regarding permutations of lists of numbers, or combinations of each letter in a string. None of those things is what I need. I need to combine blocks of text arranged in lists.

How can I create a long list of sentences containing all of the combinations of the different elements in each one of the lists of strings?

Thank you

martineau
  • 119,623
  • 25
  • 170
  • 301
skeitel
  • 271
  • 2
  • 6
  • 17

3 Answers3

2

Just a bunch of simple for loops will works. The trick is the order of print z,y,x.

list1 = ['The girl', 'The boy']
list2 = ['wears', 'touches', 'tries']
list3 = ['a red sweater', 'a blue sweater', 'a yellow sweater', 'a white sweater']

for x in list3:
    for y in list2:
        for z in list1:
            print (z,y,x)

Output;

The girl wears a red sweater
The boy wears a red sweater
The girl touches a red sweater
The boy touches a red sweater
The girl tries a red sweater
The boy tries a red sweater
The girl wears a blue sweater
The boy wears a blue sweater
The girl touches a blue sweater
The boy touches a blue sweater
The girl tries a blue sweater
The boy tries a blue sweater
The girl wears a yellow sweater
The boy wears a yellow sweater
The girl touches a yellow sweater
The boy touches a yellow sweater
The girl tries a yellow sweater
The boy tries a yellow sweater
The girl wears a white sweater
The boy wears a white sweater
The girl touches a white sweater
The boy touches a white sweater
The girl tries a white sweater
The boy tries a white sweater
GLHF
  • 3,835
  • 10
  • 38
  • 83
2

Use itertools.product, the handy tool for a cartesian product. Then join the products:

from itertools import product
lst = [' '.join(p) for p in product(list1, list2, list3)]

from pprint import pprint
pprint(lst)
['The girl wears a red sweater',
 'The girl wears a blue sweater',
 'The girl wears a yellow sweater',
 'The girl wears a white sweater',
 'The girl touches a red sweater',
 'The girl touches a blue sweater',
 'The girl touches a yellow sweater',
 'The girl touches a white sweater',
 'The girl tries a red sweater',
 'The girl tries a blue sweater',
 'The girl tries a yellow sweater',
 'The girl tries a white sweater',
 'The boy wears a red sweater',
 'The boy wears a blue sweater',
 'The boy wears a yellow sweater',
 'The boy wears a white sweater',
 'The boy touches a red sweater',
 'The boy touches a blue sweater',
 'The boy touches a yellow sweater',
 'The boy touches a white sweater',
 'The boy tries a red sweater',
 'The boy tries a blue sweater',
 'The boy tries a yellow sweater',
 'The boy tries a white sweater']
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

Using itertools' product, its as simple as :

import itertools
["{x} {y} {z}".format(x=x,y=y,z=z) for x,y,z in itertools.product(list1, list2, list3)]

And in python 3.6, you can drop the format call

[f"{x} {y} {z}" for x,y,z in itertools.product(list1, list2, list3)]
Uri Goren
  • 13,386
  • 6
  • 58
  • 110