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
Active
Viewed 38 times
-1
Pika Supports Ukraine
- 3,612
- 10
- 26
- 42
FedericoAC
- 3
- 1
1 Answers
1
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')]
Andrej Prsa
- 551
- 3
- 14