Combination of elements of different lists or tuples

Question:

I would like to get all the possible combinations between the elements of different lists or arrays. Let’s say I have L1 = [A,B] and L2 = [C,D]. If I somehow use itertools.combination for Python to look for the combination for two elements the result would be {AB,AC,AD,BC,BD,BC}. The issue here is that I just need {AC,AD,BC,BD} because they are elements from different lists. Is there a conditional or something that could help me get combinations of n elements from different lists?

Asked By: FedericoAC

||

Answers:

Perhaps itertools.product:

>>> from itertools import product
>>> L1 = ['A', 'B']
>>> L2 = ['C', 'D']
>>> list(product(L1, L2))
[('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D')]
Answered By: Andrej Prsa