57

I have two lists like:

list1 = ['square','circle','triangle']
list2 = ['red','green']

How can I create all permutations of these lists, like this:

[
  'squarered', 'squaregreen',
  'redsquare', 'greensquare',
  'circlered', 'circlegreen',
  'redcircle', 'greencircle',
  'trianglered', 'trianglegreen',
  'redtriangle', 'greentriangle'
]

Can I use itertools for this?

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
Edward
  • 917
  • 1
  • 9
  • 18

6 Answers6

108

You want the itertools.product method, which will give you the Cartesian product of both lists.

>>> import itertools
>>> a = ['foo', 'bar', 'baz']
>>> b = ['x', 'y', 'z', 'w']

>>> for r in itertools.product(a, b): print r[0] + r[1]
foox
fooy
fooz
foow
barx
bary
barz
barw
bazx
bazy
bazz
bazw

Your example asks for the bidirectional product (that is, you want 'xfoo' as well as 'foox'). To get that, just do another product and chain the results:

>>> for r in itertools.chain(itertools.product(a, b), itertools.product(b, a)):
...   print r[0] + r[1]
John Feminella
  • 303,634
  • 46
  • 339
  • 357
45
>>> import itertools
>>> map(''.join, itertools.chain(itertools.product(list1, list2), itertools.product(list2, list1)))
['squarered', 'squaregreen', 'circlered',
'circlegreen', 'trianglered', 'trianglegreen',
'redsquare', 'redcircle', 'redtriangle', 'greensquare',
'greencircle', 'greentriangle']
Nadia Alramli
  • 111,714
  • 37
  • 173
  • 152
17

How about

[x + y for x in list1 for y in list2] + [y + x for x in list1 for y in list2]

Example IPython interaction:

In [3]: list1 = ['square', 'circle', 'triangle']

In [4]: list2 = ['red', 'green']

In [5]: [x + y for x in list1 for y in list2] + [y + x for x in list1 for y in list2]
Out[5]: 
['squarered',
 'squaregreen',
 'circlered',
 'circlegreen',
 'trianglered',
 'trianglegreen',
 'redsquare',
 'greensquare',
 'redcircle',
 'greencircle',
 'redtriangle',
 'greentriangle']
Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212
11

I think what you are looking for is the product of two lists, not the permutations:

#!/usr/bin/env python
import itertools
list1=['square','circle','triangle'] 
list2=['red','green']
for shape,color in itertools.product(list1,list2):
    print(shape+color)

yields

squarered
squaregreen
circlered
circlegreen
trianglered
trianglegreen

If you'd like both squarered and redsquare, then you could do something like this:

for pair in itertools.product(list1,list2):
    for a,b in itertools.permutations(pair,2):
        print(a+b)

or, to make it into a list:

l=[a+b for pair in itertools.product(list1,list2)
   for a,b in itertools.permutations(pair,2)]
print(l)

yields

['squarered', 'redsquare', 'squaregreen', 'greensquare', 'circlered', 'redcircle', 'circlegreen', 'greencircle', 'trianglered', 'redtriangle', 'trianglegreen', 'greentriangle']
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
8

You can in any case do something like:

perms = []
for shape in list1:
  for color in list2:
    perms.append(shape+color)
extraneon
  • 23,575
  • 2
  • 47
  • 51
-1

Simple list comprehension will give the result

from itertools import product
final_list = [a + b for a, b in product(list1, list2)]
  • 1
    Incorrect, you will just have half of the result with this. A correct answer with list comprehension has already been given https://stackoverflow.com/a/1953216/10326291 – Nicolas Dufaur Dec 16 '22 at 15:39