How can I find all ways of pairing elements between two lists of equal length?
Question:
Suppose I have two input lists like:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
I want to get a list of lists of tuples, of all the potential combinations between the two – i.e., of ways to pair all the elements up. Thus, the result should be like:
[
[(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)]
]
That is: the first list shows the 1
being paired with the 4
, the 2
with the 5
, and the 3
with the 6
. There are 6 lists, corresponding to all the ways that the elements from list1
could be associated with the elements of list2
.
I think the itertools
standard library should be helpful here, but I couldn’t figure out a solution. I got this far:
list1 = [1, 2, 3]
list2 = [5, 6, 7]
print(list(itertools.product(list1, list2)))
but the result is:
[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]
It shows every possible pairing of an element from list1
with an element of list2
, but not the possible resulting lists composed from these pairing.
How can I fix the code?
Answers:
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
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
Try this:
combos=[]
for i in list1:
for j in list2:
combos.append([i,j])
print combos
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)]
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]]
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)
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')]
Suppose I have two input lists like:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
I want to get a list of lists of tuples, of all the potential combinations between the two – i.e., of ways to pair all the elements up. Thus, the result should be like:
[
[(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)]
]
That is: the first list shows the 1
being paired with the 4
, the 2
with the 5
, and the 3
with the 6
. There are 6 lists, corresponding to all the ways that the elements from list1
could be associated with the elements of list2
.
I think the itertools
standard library should be helpful here, but I couldn’t figure out a solution. I got this far:
list1 = [1, 2, 3]
list2 = [5, 6, 7]
print(list(itertools.product(list1, list2)))
but the result is:
[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]
It shows every possible pairing of an element from list1
with an element of list2
, but not the possible resulting lists composed from these pairing.
How can I fix the code?
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
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
Try this:
combos=[]
for i in list1:
for j in list2:
combos.append([i,j])
print combos
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)]
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]]
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)
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')]