-1

Say I have a function, myfunc(*iterables)

and I want to do the following:

myfunc('abc','abc','abc')

but without repeating 'abc' and using n (=3 in the above example)

I have tried:

myfunc(['abc']*n)

However this gives

myfunc(['abc', 'abc', 'abc'])

which does not work.

Is there a simple way to do this?

Lee
  • 29,398
  • 28
  • 117
  • 170

2 Answers2

4

You need to unpack the arguments list, like this

myfunc(*['abc']*n)

For example,

def myfunc(*iterables):
    print iterables

myfunc('abc', 'abc', 'abc')   # 3 Arguments
# ('abc', 'abc', 'abc')
myfunc(['abc'] * 3)           # 1 Argument with 3 items in it
# (['abc', 'abc', 'abc'],)
myfunc(*['abc'] * 3)          # Unpack the 3 element list, to pass 3 arguments
# ('abc', 'abc', 'abc')
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

You have the convert the list back to arguments:

myfunc(*['abc']*n)
Daniel
  • 42,087
  • 4
  • 55
  • 81