Python - use list as function parameters

195,599

Solution 1

You can do this using the splat operator:

some_func(*params)

This causes the function to receive each list item as a separate parameter. There's a description here: http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists

Solution 2

This has already been answered perfectly, but since I just came to this page and did not understand immediately I am just going to add a simple but complete example.

def some_func(a_char, a_float, a_something):
    print a_char

params = ['a', 3.4, None]
some_func(*params)

>> a

Solution 3

Use an asterisk:

some_func(*params)

Solution 4

You want the argument unpacking operator *.

Share:
195,599
Jonathan Livni
Author by

Jonathan Livni

python, django, C++ and other vegetables...

Updated on October 01, 2020

Comments

  • Jonathan Livni
    Jonathan Livni over 3 years

    How can I use a Python list (e.g. params = ['a',3.4,None]) as parameters to a function, e.g.:

    def some_func(a_char,a_float,a_something):
       # do stuff
    
  • inspectorG4dget
    inspectorG4dget over 13 years
    Along those lines, you can also use a dictionary: def f(a, b, c): #do stuff. mydict = {'a':1, 'b':2, 'c':3} f(**mydict)