0

I have two lists of integers. I want to make all possible combinations of elements in list-1 with elements with list-2. For Example:

List-1    List-2
1         5
2         6

I need another list of all possible combinations like:

element-1    element-2
1            5
1            6
2            5
2            6

How to do that in python?

ShadowRanger
  • 124,179
  • 11
  • 158
  • 228
Haroon S.
  • 2,363
  • 5
  • 19
  • 36

2 Answers2

2

You are looking for itertools.product():

>>> import itertools
>>> list(itertools.product([1, 2], [5, 6]))
[(1, 5), (1, 6), (2, 5), (2, 6)]
Gareth Latty
  • 82,334
  • 16
  • 175
  • 177
0

You can try itertools :

list_1=[1,2]
list_2=[5,6]

import itertools
print([i for i in itertools.product(list_1,list_2)])

output:

[(1, 5), (1, 6), (2, 5), (2, 6)]
Aaditya Ura
  • 10,695
  • 7
  • 44
  • 70