1

Does python support dynamic argument passing when I call a function?

  import itertools.product
  l = [1,2,3,95,5]
  for i in range(5):
      for n in itertools.product(l,l):
         #calculations that
         #reduce set size

I want through the iterations of i the product to be:

i=1: product(l,l)

i=2: product(l,l,l)

i=3: product(l,l,l,l)

...

If I can recall correctly the only language I know that supports that kind of functionality is PHP.

1 Answer 1

3

itertools.product accepts an optional keyword argument repeat:

So, you can do:

for n in itertools.product(l, repeat=i+1):
    ...

Alternatively, to dynamically pass argument, you can use *args (See Unpacking argument lists):

for n in itertools.product(*([l] * (i+1))):
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.