26
votes

I'm trying to figure out the best way to merge two lists into all possible combinations. So, if I start with two lists like this:

list1 = [1, 2]
list2 = [3, 4]

The resulting list will look like this:

[[[1,3], [2,4]], [[1,4], [2,3]]]

That is, it basically produces a list of lists, with all the potential combinations between the two.

I've been working through itertools, which I'm pretty sure holds the answer, but I can't come up with a way to make it act this way. The closest I came was:

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print list(itertools.product(list1, list2))

Which produced:

[(1, 5), (1, 6), (1, 7), (1, 8), (2, 5), (2, 6), (2, 7), (2, 8), (3, 5), (3, 6), (3, 7), (3, 8), (4, 5), (4, 6), (4, 7), (4, 8)]

So it does all the possible combinations of items in each list, but not all the possible resulting lists. How do I get that to happen?

EDIT: The end goal is to be able to individually process each list to determine efficiency (the actual data I'm working with is more complex). So, in the original example above, it would work something like this:

list1 = [1, 2]
list2 = [3, 4]

Get first merged list: [[1,3], [2, 4]]
    Do stuff with this list
Get second merged list: [[1,4], [2, 3]]
    Do stuff with this list

If I got the "list of lists of lists" output I described above, then I could put it into a for loop and process on. Other forms of output would work, but it seems the simplest to work with.

7
[[[1,3], [2,4]], [[1,4], [2,3]]] Why a list of lists of lists? Why not a list of lists? What are the criteria? When does an item go in the 1st or 2nd list? I would expect the expected result to be [[1,3], [2,4], [1,4], [2,3]] - Tim
That's pretty much what itertools.product is giving me (just a list of tuples instead of a list of list). I'm not trying to find all the possible combinations of items on both lists, but all the possible lists I can produce by merging the two. I'm going to add another edit to try to explain in more plain English instead of just expected code output. - GeoJunkie
Do you want all the possible interweavings of the two lists? Like what would happen during shuffling of cards. So the example [1, 2], [3, 4] would result in the sequences or so: 1 2 3 4 / 1 3 2 4 / 1 3 4 2 / 3 4 1 2 / 3 1 4 2 / 3 1 2 4. - Dan D.
Is the order important? - Padraic Cunningham
what should the output for list1 = [1, 2, 3];list2 = [5, 6, 7] be, almost all the answers below return different output than your accepted answer so it is not clear what you actually want, the product or premutations - Padraic Cunningham

7 Answers

25
votes

repeat the first list, permutate the second and zip it all together

>>> from itertools import permutations, repeat
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> list(list(zip(r, p)) for (r, p) in zip(repeat(a), permutations(b)))
[[(1, 4), (2, 5), (3, 6)],
 [(1, 4), (2, 6), (3, 5)],
 [(1, 5), (2, 4), (3, 6)],
 [(1, 5), (2, 6), (3, 4)],
 [(1, 6), (2, 4), (3, 5)],
 [(1, 6), (2, 5), (3, 4)]]

EDIT: As Peter Otten noted, the inner zip and the repeat are superfluous.

[list(zip(a, p)) for p in permutations(b)]
18
votes

The accepted answer can be simplified to

a = [1, 2, 3]
b = [4, 5, 6]
[list(zip(a, p)) for p in permutations(b)]

(The list() call can be omitted in Python 2)

9
votes

Try to use list generator to create the nested lists:

>>> [[[x,y] for x in list1] for y in list2]
[[[1, 3], [2, 3]], [[1, 4], [2, 4]]]
>>>

Or, if you want one-line list, just delete brackets:

>>> [[x,y] for x in list1 for y in list2]
[[1, 3], [1, 4], [2, 3], [2, 4]]
8
votes

As @pacholik s answer does not cover lists of different length, here is my solution, using a list comprehension with two variables:

first_list = [1, 2, 3]
second_list = ['a', 'b']

combinations = [(a,b) for a in first_list for b in second_list]

The output looks like this:

[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b'), (3, 'a'), (3, 'b')]
2
votes

Edited my code to give you your desired output.

list1 = [1,2]
list2 = [3,4]
combined = []

for a in list1:
    new_list = []
    for b in list2:
        new_list.append([a, b])
    combined.append(new_list)

print combined
2
votes

You can create a list by constructing all the permutations of two list members with this, containing the list combinations.

lst1 = [1,2]
lst2 = [3,4]

#lst = [[j,k] for j in lst1 for k in lst2] # [[1,3],[1,4],[2,3],[2,4]]
lst = [[[j,k] for j in lst1] for k in lst2] # [[[1,3],[2,3]],[[1,4],[2,4]]]
print lst
1
votes

Try this:

combos=[]
for i in list1:
      for j in list2:
          combos.append([i,j])
print combos