0

this is probably a dumb question so I'll keep it as simple as possible.

I am have a list of varying number of lists (x) and I'm trying to pass each list to the function itertools.product(), but I can't get it to work.

x = [[a, b, c], [d, e, f], [g, h, i]]
itertools.product(x[0], x[1], x[2]...)

I have tried:

itertools.product(x[n] for n in range(len(x)))
itertools.product(n for n in x)

My function would hopefully behave:

output = []
for product in itertools.product(x[n] for n in range(len(x))):
    output.append(product)
output """--> [(a, d), (a, e)...]"""

Thanks so much in advance.

Nick
  • 3
  • 1

2 Answers2

2

Use unpacking:

x = [[a, b, c], [d, e, f], [g, h, i]]
itertools.product(*x)
jedwards
  • 29,432
  • 3
  • 65
  • 92
2

Same as always.

itertools.product(*x)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358