2

I'm trying to create a cartesian product list of all possible combinations between two lists, made from a master list. But passing the "master_list" into the cartesian product function.

I want the results:

(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (1, 0) (1, 1) (1, 2) (1, 3) (1, 4)

x = [0,1]
y = [0,1,2,3,4]

This works (below) in displaying desired results:

mylist = list(itertools.product(x, y))

However, this doesn't work (below), and this is what I really need. The "master_list" list might be comprised of multiple lists dynamically. What am I missing here?:

master_list = [x, y]
mylist = list(itertools.product(master_list))

The list "master_list" is being created dynamically within the code, so I can't type something like this below. This example also works to show what I need.

mylist = list(itertools.produuct(master_list[0], master_list[1]))
Robert
  • 135
  • 2
  • 12
  • How does `master_list` actually looks like? It could just be a problem of "flattening" a *list of lists*? (converting a list of lists into a list)? – Savir Jan 02 '18 at 03:01
  • What master_list looks like should be above. It is just a list that contains x list and y list. Can you see the line that says "master_list = [x, y]"? Thank you for your help. I must be missing something obvious here. – Robert Jan 02 '18 at 03:05
  • 1
    Possible duplicate of [Get the cartesian product of a series of lists?](https://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists) – RoadRunner Jan 02 '18 at 03:13

1 Answers1

2

You need to unpack it.

list(itertools.product(*master_list)))
wwii
  • 23,232
  • 7
  • 37
  • 77