python get unique pairs from two lists 1

python get unique pairs from two lists

# note: this works for two list only
from itertools import product

l1 = [1,2,3]
l2 = [4,5,6] # works if the second list has a different lenght too

product(l1, l2)
>>> 

list(product(l1, l2))
>>> [(1, 4), (1, 5), (1, 6), 
     (2, 4), (2, 5), (2, 6), 
     (3, 4), (3, 5), (3, 6)]

Here is what the above code is Doing:
1. itertools.product() takes two lists as input and returns an iterator.
2. The iterator returned by product() can be converted into a list by passing it to the list() function.
3. The list() function takes an iterator as input and returns a list.
4. The list returned by list() contains all the possible combinations of the elements of the two lists.

Similar Posts