Subhankar Debnath
Subhankar Debnath

Reputation: 21

How to find different combinations between two list?

There are two lists. list_1=[a1,b1,c1,d1] list_2=[a2,b2,c2,d2]

Conditions are (i) there must be four elements in each of the combinations and (ii) combinations should contain one element of a (i.e. either a1 or a2),one element of b (i.e. either b1 or b2),one element of c (i.e. either c1 or c2) and one element of d (i.e. either d1 or d2).

Please help me to find out different combinations by using python 3x.

Upvotes: 2

Views: 51

Answers (1)

iz_
iz_

Reputation: 16633

You can use itertools.product:

from itertools import product

list_1 = ['a1','b1','c1','d1']
list_2 = ['a2','b2','c2','d2']

result = list(product(*zip(list_1, list_2)))

print(result)

[('a1', 'b1', 'c1', 'd1'), ('a1', 'b1', 'c1', 'd2'), ('a1', 'b1', 'c2', 'd1'), ('a1', 'b1', 'c2', 'd2'), ('a1', 'b2', 'c1', 'd1'), ('a1', 'b2', 'c1', 'd2'), ('a1', 'b2', 'c2', 'd1'), ('a1', 'b2', 'c2', 'd2'), ('a2', 'b1', 'c1', 'd1'), ('a2', 'b1', 'c1', 'd2'), ('a2', 'b1', 'c2', 'd1'), ('a2', 'b1', 'c2', 'd2'), ('a2', 'b2', 'c1', 'd1'), ('a2', 'b2', 'c1', 'd2'), ('a2', 'b2', 'c2', 'd1'), ('a2', 'b2', 'c2', 'd2')]

Upvotes: 5

Related Questions