-2

Say I have this list of lists:

lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']]

How can I make an iteration that outputs me all possible combinations within len(lst_of_lsts)

Desired Output:

comb1 = ['1-1', '2-1', '3-1']
comb2 = ['1-2', '2-1', '3-1']
comb3 = ['1-3', '2-1', '3-1']
comb4 = ['1-1', '2-2', '3-1']
comb5 = ['1-2', '2-2', '3-1']
comb6 = ['1-3', '2-2', '3-1']
comb7 = ['1-1', '2-3', '3-1']
comb8 = ['1-2', '2-3', '3-1']
comb9 = ['1-3', '2-3', '3-1']
comb10 = ['1-1', '2-1', '3-2']
comb11 = ['1-1', '2-1', '3-3']
comb12 = ['1-1', '2-2', '3-2']

etc... etc... etc...

you get the idea..

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 2
    What you want is called the "cartesian product" https://en.wikipedia.org/wiki/Cartesian_product. "Combinations" is a little bit different of a concept. – James Pringle Nov 22 '20 at 14:04

2 Answers2

2

I think you are looking for itertools.product

>>> import itertools
>>> res = list(itertools.product(*lst_of_lsts))
>>> res
[('1-1', '2-1', '3-1'), ('1-1', '2-1', '3-2'), ('1-1', '2-1', '3-3'), ('1-1', '2-2', '3-1'), ('1-1', '2-2', '3-2'), ('1-1', '2-2', '3-3'), ('1-1', '2-3', '3-1'), ('1-1', '2-3', '3-2'), ('1-1', '2-3', '3-3'), ('1-2', '2-1', '3-1'), ('1-2', '2-1', '3-2'), ('1-2', '2-1', '3-3'), ('1-2', '2-2', '3-1'), ('1-2', '2-2', '3-2'), ('1-2', '2-2', '3-3'), ('1-2', '2-3', '3-1'), ('1-2', '2-3', '3-2'), ('1-2', '2-3', '3-3'), ('1-3', '2-1', '3-1'), ('1-3', '2-1', '3-2'), ('1-3', '2-1', '3-3'), ('1-3', '2-2', '3-1'), ('1-3', '2-2', '3-2'), ('1-3', '2-2', '3-3'), ('1-3', '2-3', '3-1'), ('1-3', '2-3', '3-2'), ('1-3', '2-3', '3-3')]
James Pringle
  • 1,079
  • 6
  • 15
abc
  • 11,579
  • 2
  • 26
  • 51
1

I think what you want isn't technically the list of combinations (where order doesn't matter), but rather the product of the given lists, li.e. all lists where the first element comes from the first list, the second elements comes from the second list, etc.

This (and many other useful related functions) are available in the itertools library, particularly itertools.product:

>>> from itertools import product
>>> list(product(*lst_of_lsts))
[('1-1', '2-1', '3-1'), ('1-1', '2-1', '3-2'), ('1-1', '2-1', '3-3'), ('1-1', '2-2', '3-1'), ('1-1', '2-2', '3-2'), ('1-1', '2-2', '3-3'), ('1-1', '2-3', '3-1'), ('1-1', '2-3', '3-2'), ('1-1', '2-3', '3-3'), ('1-2', '2-1', '3-1'), ('1-2', '2-1', '3-2'), ('1-2', '2-1', '3-3'), ('1-2', '2-2', '3-1'), ('1-2', '2-2', '3-2'), ('1-2', '2-2', '3-3'), ('1-2', '2-3', '3-1'), ('1-2', '2-3', '3-2'), ('1-2', '2-3', '3-3'), ('1-3', '2-1', '3-1'), ('1-3', '2-1', '3-2'), ('1-3', '2-1', '3-3'), ('1-3', '2-2', '3-1'), ('1-3', '2-2', '3-2'), ('1-3', '2-2', '3-3'), ('1-3', '2-3', '3-1'), ('1-3', '2-3', '3-2'), ('1-3', '2-3', '3-3')]

Note the use of * since product expects the outer list as positional arguments, and the use of list (for nicer illustration) since product—as requested—returns an iterator.

Jan Pöschko
  • 5,412
  • 1
  • 28
  • 28
  • All righty, now since this is a list of tuples. Can I for loop each tuple and assign the values to dictionary keys? for example: {var1: 1-1, var2: 2-1, var3: 3-1} and so on respectively? Given the fact I have a dictionary with the len of keys == len of the list of lists. So, 3 vars for 3 values BTW, thanks for the answer :) – Robert Tiger Nov 22 '20 at 14:17
  • Yes, you can, e.g. `for (var1, var2, var3) in product(*lst_of_lsts): ...` – Jan Pöschko Nov 22 '20 at 16:54