Fix Python – Pass a list to a function to act as multiple arguments [duplicate]

Question

Asked By – AP257

In a function that expects a list of items, how can I pass a Python list item without getting an error?

my_list = ['red', 'blue', 'orange']
function_that_needs_strings('red', 'blue', 'orange') # works!
function_that_needs_strings(my_list) # breaks!

Surely there must be a way to expand the list, and pass the function 'red','blue','orange' on the hoof? I think this is called ‘unpacking’.

Now we will see solution for issue: Pass a list to a function to act as multiple arguments [duplicate]


Answer

function_that_needs_strings(*my_list) # works!

You can read all about it here.

This question is answered By – Jochen Ritzel

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0