0

Suppose I have two lists ['a', 'b'] and ['m', 'c']. Is there a quick and dirty way to "multiply" them in such a way that the resulting array would be the list of combinations of each element in both arrays, i.e.,

[ ['a', 'm'], ['a', 'c'], ['b', 'm'], ['b', 'c'] ]
Mariska
  • 1,913
  • 5
  • 26
  • 43

1 Answers1

2

Use a comprehension to iterate the two lists and pair:

list1 = ['a','b']
list2 = ['m','c']    

list3 = [[a, b] for a in list1 for b in list2]

Out: [['a', 'm'], ['a', 'c'], ['b', 'm'], ['b', 'c']]
Bob Hopez
  • 773
  • 4
  • 10
  • 28
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70