-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?

atomh33ls
  • 26,470
  • 23
  • 104
  • 159

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
  • 221,210
  • 51
  • 432
  • 478
1

You have the convert the list back to arguments:

myfunc(*['abc']*n)
Daniel
  • 40,885
  • 4
  • 53
  • 79