pythonlisttuplescombinationspython-itertools

Python combinations between 3 lists with one depending on one list


I have two lists and a third list that depends on the first one (if l1 is 1, then the element to take in l3 is 11 or 111, if l2 is 2, then l3 to take is 22, or 222).

l1 = [1, 2]
l2 = [3, 4]

l3 = [[11, 111], [22, 222]] # the numbers are calculated for each element of l1

What I would like is to create a combination resulting in:

[(1,3,11), (1,3,111), (1,4,11), (1,4,111), (2,3,22), (2,3,222), (2,4,22), (2,4,222)]

Do you have any suggestion?


Solution

  • Thanks all! In the end, I ended up with this:

    combo = []
    for i in range(len(l1)):
        for j in range(len(l2)):
            for k in range(len(l3[i])):
                combo.append((l1[i], l2[j], l3[i][k]))
                
    combo