brando_
brando_

Reputation: 3

Combination of List and Nested List by Index

The output of my script is a list and a nested list. I would like to get the combinations of the two lists by index. In this instance, I have the following two lists:

x = [0, 1, 2, 3]

y = [[0, 1, 2, 3],
 [0, 1, 2, 3, 4, 5, 6, 7, 8],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4, 5, 6, 7, 8]]

The desired output should look something like this.

[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 
7), (1, 8), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), 
(3, 6), (3, 7), (3, 8)]

I've looked at many posts about itertools.combinations and itertools.product, but I cannot find anything about looping and combining at the same time, which I think would be the approach to the problem. I want to get all combinations x[0] and y[0], then x[1] and y[1], etc.

Upvotes: 0

Views: 104

Answers (2)

NIKHIL KULSHRESTHA
NIKHIL KULSHRESTHA

Reputation: 134

It seems you are going to do the catidion multiplication of two array. Here is the reference check and let me know if worked for you. Cartesian product of x and y array points into single array of 2D points

Upvotes: 0

Mitchell Olislagers
Mitchell Olislagers

Reputation: 1827

You can do this with a list comprehension.

x = [0, 1, 2, 3]

y = [[0, 1, 2, 3],
 [0, 1, 2, 3, 4, 5, 6, 7, 8],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4, 5, 6, 7, 8]]

final = [(i,j) for i in x for j in y[i]]

Upvotes: 1

Related Questions