Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split or Extract Strings into Arguments of Function?

Tags:

python

string

Lets say I have a function that takes string arguments. But I want to dynamically generate them. There does not seem to be a way to plug this in easily. How is this done? See my example here

i_take_strings('one', 'two', 'and_the_letter_C')

s = 'one two and_the_letter_c'

i_take_strings(x for x in s.split()) #python thinks I'm retarded with this attempt
like image 625
1Up Avatar asked Mar 15 '23 05:03

1Up


1 Answers

s.split() already returns a list so you can pass it to your function as variable arguments by prepending * like follows:

i_take_strings(*s.split())
like image 188
Ozgur Vatansever Avatar answered Mar 23 '23 05:03

Ozgur Vatansever