Search code examples
python-3.xlistpermute

Get the combination of 2 lists


I'm reposting this question because I was told that there is a solution for that in the last post.

I have 2 lists:

list1 = ["foo", "bar", "lorem"]
list2 = ["X", "Y"]

I want to have the possible combinations from these 2 lists, meaning:

[["foo", "bar", "lorem"],
 ["foo", "bar", "loremX"],
 ["foo", "barX", "loremX"],
 ["fooX", "bar", "loremX"],
 ["fooX", "barX", "loremX"],
 ["foo", "barX", "lorem"],
 ["fooX", "barX", "lorem"],
 ["fooX", "bar", "lorem"],

 ["foo", "bar", "lorem"],
 ["foo", "bar", "loremY"],
 ["foo", "barY", "loremY"],
 ["fooY", "bar", "loremY"],
 ["fooY", "barY", "loremY"],
 ["foo", "barY", "lorem"],
 ["fooY", "barY", "lorem"],
 ["fooY", "bar", "lorem"]]

Hope I didn't miss any combination.

Kinda lost with this one.

It probably should be something with itertools.combinations_with_replacement

Thanks.

EDIT

First of all, thanks to @titusarmah99 for a great answer. I managed to take his second-and-very-simple solution and make it generic:

import itertools

list1 = ["foo", "bar", "lorem"]
list2 = ["X", "Y"]
list2new = [""] + list2
newList = [[list1[i]+list2new[j] for j in range(len(list2new))] for i in range(len(list1))]

for index in range(1, len(list2) + 1):
    for c in itertools.product([0,index],repeat=len(list1)):
        tmp = [newList[i][c[i]] for i in range(len(c))]
        print(tmp)


Solution

  • The key here is to use rotation. There are many ways to rotate an array, i will be using deque.

    from collections import deque
    
    list1 = ["foo", "bar", "lorem"]
    list2 = ["X", "Y"]
    list2new = [""] + list2
    print(list2new) # ['', 'X', 'Y']
    
    newList = [[list1[i]+list2new[j] for j in range(len(list2new))] for i in range(len(list1))]
    print(newList)
    # [['foo', 'fooX', 'fooY'], ['bar', 'barX', 'barY'], ['lorem', 'loremX', 'loremY']]
    
    d = deque([0,0,0,1,1,1])
    for it in range(2*len(list1)):
        tmp = [newList[i][d[i]] for i in range(len(list1))]
        print(tmp) #
        d.rotate(1)
    # ['foo', 'bar', 'lorem']
    # ['fooX', 'bar', 'lorem']
    # ['fooX', 'barX', 'lorem']
    # ['fooX', 'barX', 'loremX']
    # ['foo', 'barX', 'loremX']
    # ['foo', 'bar', 'loremX']
    
    

    After this we can repeat the process for rest of values in list2.

    for x in range(1,len(list2new)):
        d = deque([0]*len(list1)+[x]*len(list1))
        d.rotate(1)
        for it in range(2*len(list1)-1): #
            tmp = [newList[i][d[i]] for i in range(len(list1))]
            print(tmp) #
            d.rotate(1)
    # ['fooX', 'bar', 'lorem']
    # ['fooX', 'barX', 'lorem']
    # ['fooX', 'barX', 'loremX']
    # ['foo', 'barX', 'loremX']
    # ['foo', 'bar', 'loremX']
    # ['fooY', 'bar', 'lorem']
    # ['fooY', 'barY', 'lorem']
    # ['fooY', 'barY', 'loremY']
    # ['foo', 'barY', 'loremY']
    # ['foo', 'bar', 'loremY']
    

    This will not contain ['foo', 'bar', 'lorem'] so add it manually.

    EDIT: Seems, like you edited the question. To get all possible combinations, just use newList and itertools.product

    for c in itertools.product([0,1],repeat=3):
        tmp = [newList[i][c[i]] for i in range(len(c))]
        print(tmp) #