0

I need a way to get all the possible combonations of two lists. I looked into map(), but it only loops through the first array. I tried to do a map function inside a map function, but I couldnt figure out how. Heres an example:

>>> a=[1,2]
>>> b=[3,4]
>>> functionToGetCombonations(a,b)
[1,3]
[1,4]
[2,3]
[2,4]

Thanks in advance

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by mattloto
1

Check itertools' product.

import itertools

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
x = itertools.product(list_1, list_2)
for y in x:
   print y

"""   My result
(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
"""
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.