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