127

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
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
  • Related: [Python 3 Annotations: Type hinting a list of a specified type (PyCharm)](https://stackoverflow.com/questions/24853923/python-3-annotations-type-hinting-a-list-of-a-specified-type-pycharm) – Stevoisiak Feb 27 '18 at 20:36

4 Answers4

185

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

Neil Vass
  • 5,251
  • 2
  • 22
  • 25
72

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
Michael David Watson
  • 3,028
  • 22
  • 36
17

Use an asterisk:

some_func(*params)
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
12

You want the argument unpacking operator *.

btilly
  • 43,296
  • 3
  • 59
  • 88