How to Pass a List to a Function in Python
- Pass a List to a Function in Python
- Pass a List to a Python Function Just Like Any Other Data Type
- Difference Between Passing and Unpacking a List in Python
- Conclusion
In Python, sending a list to a function is just like passing any other form of data. Let’s explore a little more into this subject.
Pass a List to a Function in Python
We will define a function testing
with a single parameter, flavor
. Then, we pass an argument named Cherry
while calling the function.
This argument goes to the parameter variable flavor
, which the function can then use. See the example below.
Code:
def testing(flavor):
print("You chose:", flavor)
testing("Cherry")
Output:
You chose: Cherry
Pass a List to a Python Function Just Like Any Other Data Type
Python lists are like any other Python object that we can pass into a function as a simple variable. We have a function enjoy
with a hobbies
parameter in the code sample below.
Outside the function block, we define a list hobbies_list
. While calling the function enjoy
, we pass this variable, hobbies_list
, as an argument.
This list goes to the parameter variable hobbies
, and thus, the function can use the value of this list.
Code:
def enjoy(hobbies): # or def enjoy(hobbies=[]):
for hobby in hobbies:
print(hobby)
hobbies_list = ["art", "dance", "sing"]
enjoy(hobbies_list)
Output:
art
dance
sing
See how the enjoy
function gets the value of the list, and the for
loop inside it prints all the list items. Sometimes, you will also see the assignment of square brackets []
to the parameter variable while passing a list to a function.
Difference Between Passing and Unpacking a List in Python
In Python, we can use *args
to pass a variable number of arguments to a function. Now, since a list has multiple values, people tend to use *args
as the parameter variable for a list argument so that all the values of the list are taken care of.
When we define *args
as the parameter variable, we signal the function to wait for a variable number of arguments. Passing the elements of a list as multiple arguments is similar to unpacking a list.
Code:
def subjects(*args):
for subject in args:
print("The subject name is ", subject)
names = ["mathematics", "science", "history"]
subjects(names)
Output:
The subject name is ['mathematics', 'science', 'history']
You will see the difference better if you compare this output with the below code.
Code:
def subjects(args):
for subject in args:
print("The subject name is ", subject)
names = ["mathematics", "science", "history"]
subjects(names)
Output:
The subject name is mathematics
The subject name is science
The subject name is history
Notice how the output changes based on *args
. If *args
is used to pass a list to a Python function, we might not get the expected results.
As a result, it’s important to select the appropriate syntax based on the requirements.
Conclusion
In this article, we learned about passing a list to a function in Python. We saw how we could pass a list to a function in Python just like we pass any other data type.
We further understood the difference between passing and unpacking lists as multiple arguments.