Jonathan Livni
Jonathan Livni

Reputation: 107092

Python - use list as function parameters

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

Upvotes: 127

Views: 220017

Answers (4)

Michael David Watson
Michael David Watson

Reputation: 3071

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

Upvotes: 72

btilly
btilly

Reputation: 46409

You want the argument unpacking operator *.

Upvotes: 12

Neil Vass
Neil Vass

Reputation: 5341

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

Upvotes: 186

Mark Byers
Mark Byers

Reputation: 838216

Use an asterisk:

some_func(*params)

Upvotes: 17

Related Questions