2

I have a Python list consisting of integers:

a = [1, 2, 3]

I want to pass the items of this list as arguments to a function, and they must be strings:

myfunc("1", "2", "3")

How can I do it?

2 Answers 2

12

So... we use the * operator to use a sequence as multiple arguments for a function call; and we want to convert each argument to a string. The conversion is most obviously and simply done by just passing the value to the builtin str; we can then just map that conversion function onto the list. These are all elementary techniques and all we have to do is put them together:

myfunc(*map(str, a))
Sign up to request clarification or add additional context in comments.

3 Comments

The edit is too small for me to make but the parentheses around map() are extraneous.
Why use an extra pair of brackets?
Just force of habit, since in the code I've written lately, the * is typically applied to a generator expression. I removed the extra parens and added some explanation.
3

* keyword can unpacking the argument list.

a = [1, 2, 3]
myfunc(*(str(l) for l in a))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.