-1

I want to use a function that takes *args:arrays as arguments (so f(a,b,c,...,z) where a,b,...z are arrays. I have my variables stored as array_vars = [a,b,c,...,z].

How do I transform array_vars so that the function f understands ?

Chapo
  • 2,563
  • 3
  • 30
  • 60

2 Answers2

0

You just need to * operator to unpack the items from a list.

f(*array_vars)

Example

>>> def f(*args):
...     for x in args:
...         print(x)
... 
>>> f(1, 2, 3)
1
2
3
>>> f(*[1, 2, 3])
1
2
3
jpuriol
  • 362
  • 2
  • 14
0

With * you can unpack arguments from a list or tuple and ** unpacks arguments from a dict.

>>> range(3, 6)             # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)            # call with arguments unpacked from a list
[3, 4, 5]
Beyhan Gul
  • 1,191
  • 1
  • 15
  • 25