Python Forum

Full Version: Product expression.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What I want to do it to use Product from itertools.

I want the length of the product to be variable.

so for example...
var = 'AB'

If I had product(var, var)
I would get
AA
AB
BA
BB

If product(var, var, var)
AAA
AAB
ABA etc...

what if want to make it so I can just tell product I want var 2x or 3 x or 4x such as:
product(var, var)
product(var, var, var)
product(var, var, var, var)

how can I write a simple function so that I send a number for how many times I want to feed var into the product function then get my object back?

Thanks

UPDATE:

Never mind I figured out I can feed a list of lists into Product with a *.
example:
var = [['A', 'B'], ['A', 'B']]
myObject = product(*var)
Did you check the docs? Did you notice the optional repeat parameter?
From the docs
Quote:To compute the product of an iterable with itself, specify the number of repetitions with the optional repeat keyword argument. For example, product(A, repeat=4) means the same as product(A, A, A, A).