0
list1 = [1,1,2,4]
list2 = [2,3,5,6]

i would like get all combination like [1,3,5,6], [1,3,5,6] like all combination in python

My question is like list1=[1,2,3,4] list2=[5,6,7,8] I need to see the lists like [1,5,6,7] [5,2,7,8] meaning all possible combination of 2 list.please help

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Memi Sen
  • 1
  • 2
  • Does this answer your question? [combinations between two lists?](https://stackoverflow.com/questions/12935194/combinations-between-two-lists) – erncyp Jun 30 '20 at 08:50
  • I have tried below x = [1, 2, 3,9] y = [4, 5,8,7] new_array = np.array(np.meshgrid(x, y)).T.reshape(-1,4) The Result is Combine array: [[1 4 1 5] [1 8 1 7] [2 4 2 5] [2 8 2 7] [3 4 3 5] [3 8 3 7] [9 4 9 5] [9 8 9 7]] But my Requirement is like [1,5,8,7][4,2,8,7] like on – Memi Sen Jul 06 '20 at 09:06
  • Does this answer your question? [How to get the Cartesian product of multiple lists](https://stackoverflow.com/questions/533905/how-to-get-the-cartesian-product-of-multiple-lists) – Karl Knechtel Mar 02 '23 at 03:22

2 Answers2

1

Like this IIUC:

>>> print([[i] + list2[1:] for i in list1])
[[1, 3, 5, 6], [1, 3, 5, 6], [2, 3, 5, 6], [4, 3, 5, 6]]
ScootCork
  • 3,411
  • 12
  • 22
  • My question is like list1=[1,2,3,4] list2=[5,6,7,8] I need to see the lists like [1,5,6,7] [5,2,7,8] meaning all possible combination of 2 list.please help. – Memi Sen Jun 30 '20 at 09:14
  • It is not quite clear to me what is expected. Please adjust your question, explain the steps from input to result and give full output examples. See https://stackoverflow.com/help/minimal-reproducible-example on how to best do this. – ScootCork Jun 30 '20 at 09:21
0

I think this is what you are looking for:

import itertools
list1 = [1,1,2,4]
list2 = [2,3,5,6]
combination_list = list(itertools.combinations(list1 + list2, 4))

this joins the two lists into one list (i.e, [1,1,2,4,2,3,5,6]) and takes all the 4 element combinations.

erncyp
  • 1,649
  • 21
  • 23