Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - use list as function parameters

People also ask

Can the function take a list as parameter Python?

In Python, functions can take either no arguments, a single argument, or more than one argument. We can pass a string, integers, lists, tuples, a dictionary etc.

How do you pass lists as parameters in Python?

In Python, we can easily expand the list, tuple, dictionary as well as we can pass each element to the function as arguments by the addition of * to list or tuple and ** to dictionary while calling function.

Can you call a list in a function Python?

Python lists are like any other Python object that we can pass into a function as a simple variable.

How do you pass a number and a list as an argument in Python?

Use the * Operator in Python The * operator can unpack the given list into separate elements that can later be tackled as individual variables and passed as an argument to the function.


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


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

Use an asterisk:

some_func(*params)

You want the argument unpacking operator *.