Pass a list to a function to act as multiple arguments
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC L Beethoven - Piano Sonata No 8 in C
--
Chapters
00:00 Question
00:32 Accepted answer (Score 358)
00:49 Answer 2 (Score 49)
01:38 Answer 3 (Score 19)
02:00 Thank you
--
Full question
https://stackoverflow.com/questions/3480...
Accepted answer links:
[Unpacking Argument Lists - The Python Tutorial]: https://docs.python.org/tutorial/control...
Answer 2 links:
[call expression documentation]: https://docs.python.org/2/reference/expr...
[equivalent syntax]: https://stackoverflow.com/questions/3690...
Answer 3 links:
[PEP 448 - Additional Unpacking Generalizations]: https://docs.python.org/3/whatsnew/3.5.h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #list #parameterpassing
#avk47
--
Track title: CC L Beethoven - Piano Sonata No 8 in C
--
Chapters
00:00 Question
00:32 Accepted answer (Score 358)
00:49 Answer 2 (Score 49)
01:38 Answer 3 (Score 19)
02:00 Thank you
--
Full question
https://stackoverflow.com/questions/3480...
Accepted answer links:
[Unpacking Argument Lists - The Python Tutorial]: https://docs.python.org/tutorial/control...
Answer 2 links:
[call expression documentation]: https://docs.python.org/2/reference/expr...
[equivalent syntax]: https://stackoverflow.com/questions/3690...
Answer 3 links:
[PEP 448 - Additional Unpacking Generalizations]: https://docs.python.org/3/whatsnew/3.5.h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #list #parameterpassing
#avk47
ACCEPTED ANSWER
Score 367
function_that_needs_strings(*my_list) # works!
You can read all about it here: Unpacking Argument Lists - The Python Tutorial
ANSWER 2
Score 51
Yes, you can use the *args (splat) syntax:
function_that_needs_strings(*my_list)
where my_list can be any iterable; Python will loop over the given object and use each element as a separate argument to the function.
See the call expression documentation.
There is a keyword-parameter equivalent as well, using two stars:
kwargs = {'foo': 'bar', 'spam': 'ham'}
f(**kwargs)
and there is equivalent syntax for specifying catch-all arguments in a function signature:
def func(*args, **kw):
# args now holds positional arguments, kw keyword arguments
ANSWER 3
Score 20
Since Python 3.5 you can unpack unlimited amount of lists.
PEP 448 - Additional Unpacking Generalizations
So this will work:
a = ['1', '2', '3', '4']
b = ['5', '6']
function_that_needs_strings(*a, *b)