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

Recommended Answers

All 2 Replies

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)
"""

thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.