0

I need to use the itertools.product() in Python, but the given parameters cannot be fixed, cause previously I'm loading an empty array with subarrays, using a procedure that doesn't always load the same number of subarrays.

So, let's put an example. Let's suppose that I have a variable called arraysVar. This procedure could load several subarrays as I said, for example:

[[True,False],['a','b']]

Or:

[[True,False],['a','b'],['weak','medium','strong']]

Or whatever.. It just has several arrays inside with different values inside them.

Then I want to do the product of all them with itertools, but I only know to pass a fixed number of parameters by saying:

itertools.product(arraysVar[0],arraysVar[1], ...)

But since the number of subarrays is variable, I want to pass as many parameters as subarrays inside arraysVar. How could I do it?

I tried this:

itertools.product([arraysVar[i] for i in range(len(arraysVar))])

But it doesn't work properly cause it considers it's getting only 1 parameter.

Thanks in advance.

  • 2
    `itertools.product(*arraysVar)`? – Christian Dean Jul 17 '17 at 19:42
  • @ChristianDean I think you should post this as an answer – AGN Gazer Jul 17 '17 at 19:45
  • @AGNGazer: they can't because the post is already closed as a duplicate. – Martijn Pieters Jul 17 '17 at 19:45
  • @AGNGazer Thanks, but I really shouldn't. This question already has good answers on the duplicate post linked. It's generally frowned upon to answer obvious duplicates that have been asked many times before. And besides even if I did want to, I couldn't'; Martijn already closed the post, no new answers can be added. – Christian Dean Jul 17 '17 at 19:47
  • Sorry, I didi not know it was a duplicate but I should have known better that such a fundamental question should have already had an answer on SO. – AGN Gazer Jul 17 '17 at 19:49
  • @AGNGazer No harm done. Just wanted to let you know for future reference. – Christian Dean Jul 17 '17 at 19:51
  • Wow.. Thank you so much Christian!! That definitely worked! And I'm so sorry for the duplicate, but I already searched for it and didn't find anything... That's the first thing I do cause it takes less time than writting a new post in fact, so next time I'll check it out more carefully. – Mario E. U. Jul 17 '17 at 19:52

0 Answers0