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
  • 143,180
  • 12
  • 188
  • 271
Haroon S.
  • 2,533
  • 6
  • 20
  • 39

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
  • 86,389
  • 17
  • 178
  • 183
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
  • 12,007
  • 7
  • 50
  • 88