0

I need to make couple for elements from two lists (two lists are different size)
Ex:

x = ([3,2], [4,6,-8])
y = ([-5,1,4], [13,9], [7,5,-1,10])

Result:

new_list = ([3, -5], [3, 1], [3, 4], [3, 13], [3, 9], [3, 7], [3, 5], [3, -1],[3, 10], [2, -5], [2, 1] ......<sorry, there're too many>.... [-8, 5], [-8, -1], [-8, 10])

Thanks for support!

David Parks
  • 30,789
  • 47
  • 185
  • 328
Znone
  • 51
  • 4

4 Answers4

0

In two steps, 1) flatten your list of lists, 2) itertools.product

Flatten your list of lists: How to make a flat list out of a list of lists

flat_list = itertools.chain(*iterable_of_lists)

Use itertools to create the product of the two lists.

itertools.product(x, y)

Example from OP:

import itertools

x = ([3,2], [4,6,-8])
y = ([-5,1,4], [13,9], [7,5,-1,10])
x_flat = itertools.chain(*x)
y_flat = itertools.chain(*y)
list(itertools.product(x_flat, y_flat))

result:

[(3, -5), (3, 1), (3, 4), (3, 13), (3, 9), (3, 7), (3, 5), (3, -1), (3, 10), (2, -5), (2, 1), (2, 4), (2, 13), (2, 9), (2, 7), (2, 5), (2, -1), (2, 10), (4, -5), (4, 1), (4, 4), (4, 13), (4, 9), (4, 7), (4, 5), (4, -1), (4, 10), (6, -5), (6, 1), (6, 4), (6, 13), (6, 9), (6, 7), (6, 5), (6, -1), (6, 10), (-8, -5), (-8, 1), (-8, 4), (-8, 13), (-8, 9), (-8, 7), (-8, 5), (-8, -1), (-8, 10)]
David Parks
  • 30,789
  • 47
  • 185
  • 328
0

Your x, y and new_list in the examples are actually tuples and not lists, because you use () instead of [].

Based on your expected result, I assume that you don't really need lists of lists here, you just take flat lists:

x = [3,2,4,6,-8]
y = [-5,1,4,13,9,7,5,-1,10]

And find each combination between x and y. Which can be done using itertools.product

Expurple
  • 877
  • 6
  • 13
0

Using itertools chain and product:

from itertools import product, chain

new_list = list(product(chain(*x), chain(*y)))
[(3, -5), (3, 1), (3, 4), (3, 13), (3, 9), (3, 7), (3, 5), (3, -1), (3, 10), (2, -5), (2, 1), (2, 4), (2, 13), (2, 9), (2, 7), (2, 5), (2, -1), (2, 10), (4, -5), (4, 1), (4, 4), (4, 13), (4, 9), (4, 7), (4, 5), (4, -1), (4, 10), (6, -5), (6, 1), (6, 4), (6, 13), (6, 9), (6, 7), (6, 5), (6, -1), (6, 10), (-8, -5), (-8, 1), (-8, 4), (-8, 13), (-8, 9), (-8, 7), (-8, 5), (-8, -1), (-8, 10)]
Pedro Maia
  • 2,666
  • 1
  • 5
  • 20
0

First of all you have two tuples containing lists, not lists of numbers and the expected result is a tuple containing two-length lists. So, you have to convert those tuples of lists to simple tuples first, following this solution (with a little change): https://stackoverflow.com/a/952952/11882999

x = ([3,2], [4,6,-8])
y = ([-5,1,4], [13,9], [7,5,-1,10])
x_tuple = tuple(item for sublist in x for item in sublist)
y_tuple = tuple(item for sublist in y for item in sublist)

Then following this solution (with a little change), you can calculate the combinations of two tuples easily: https://stackoverflow.com/a/39064769/11882999

result = tuple([x, y] for x in x_tuple for y in y_tuple)

>> ([3, -5], [3, 1], [3, 4], ..., [-8, 5], [-8, -1], [-8, 10])
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 20 '21 at 17:21