- How to pass an array or a list into a function in python ?
- Pass a matrix in a function
- Pass a list in a function
- References
- How to pass an array or a list into a function in python ?
- Pass a matrix in a function
- Pass a list in a function
- References
- Python Passing a List as an Argument
- Example
- Related Pages
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- Pass a List to a Function to act as Multiple Arguments
- Python List
- Pass a List to a Function as Multiple Arguments
- Example:
- Example:
- Conclusion
- Python Passing a List as an Argument
- Example
- Related Pages
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
How to pass an array or a list into a function in python ?
Examples of how to pass an array or list as an argument of a function in python:
Pass a matrix in a function
In python, it is possible to pass a matrix as an argument of a function, example:
>>> import numpy as np
>>> def function( x ):
. return 0.5 * x + 2
.
>>> x = np.arange(0,10,0.1)
>>> y = function(x)
>>> y
array([ 2. , 2.05, 2.1 , 2.15, 2.2 , 2.25, 2.3 , 2.35, 2.4 ,
2.45, 2.5 , 2.55, 2.6 , 2.65, 2.7 , 2.75, 2.8 , 2.85,
2.9 , 2.95, 3. , 3.05, 3.1 , 3.15, 3.2 , 3.25, 3.3 ,
3.35, 3.4 , 3.45, 3.5 , 3.55, 3.6 , 3.65, 3.7 , 3.75,
3.8 , 3.85, 3.9 , 3.95, 4. , 4.05, 4.1 , 4.15, 4.2 ,
4.25, 4.3 , 4.35, 4.4 , 4.45, 4.5 , 4.55, 4.6 , 4.65,
4.7 , 4.75, 4.8 , 4.85, 4.9 , 4.95, 5. , 5.05, 5.1 ,
5.15, 5.2 , 5.25, 5.3 , 5.35, 5.4 , 5.45, 5.5 , 5.55,
5.6 , 5.65, 5.7 , 5.75, 5.8 , 5.85, 5.9 , 5.95, 6. ,
6.05, 6.1 , 6.15, 6.2 , 6.25, 6.3 , 6.35, 6.4 , 6.45,
6.5 , 6.55, 6.6 , 6.65, 6.7 , 6.75, 6.8 , 6.85, 6.9 , 6.95])
x is a matrix and the function returns a new matrix y.
Note: it will return an error if a math function is used:
>>> import numpy as np
>>> import math
>>> def function( x ):
. return math.cos(x)
.
>>> y = function(x)
Traceback (most recent call last):
File "", line 1, in
File "", line 2, in function
TypeError: only length-1 arrays can be converted to Python scalars
since the math function do not work with matrix. It is necessary to use numpy.cos(x) instead:
>>> import numpy as np
>>> def function( x ):
. return np.cos(x)
.
>>> y = function(x)
>>> y
array([ 1. , 0.99500417, 0.98006658, 0.95533649, 0.92106099,
0.87758256, 0.82533561, 0.76484219, 0.69670671, 0.62160997,
0.54030231, 0.45359612, 0.36235775, 0.26749883, 0.16996714,
0.0707372 , -0.02919952, -0.12884449, -0.22720209, -0.32328957,
-0.41614684, -0.5048461 , -0.58850112, -0.66627602, -0.73739372,
-0.80114362, -0.85688875, -0.90407214, -0.94222234, -0.97095817,
-0.9899925 , -0.99913515, -0.99829478, -0.98747977, -0.96679819,
-0.93645669, -0.89675842, -0.84810003, -0.79096771, -0.7259323 ,
-0.65364362, -0.57482395, -0.49026082, -0.40079917, -0.30733287,
-0.2107958 , -0.11215253, -0.01238866, 0.08749898, 0.18651237,
0.28366219, 0.37797774, 0.46851667, 0.55437434, 0.63469288,
0.70866977, 0.77556588, 0.83471278, 0.88551952, 0.92747843,
0.96017029, 0.98326844, 0.9965421 , 0.99985864, 0.99318492,
0.97658763, 0.95023259, 0.91438315, 0.86939749, 0.8157251 ,
0.75390225, 0.68454667, 0.60835131, 0.52607752, 0.43854733,
0.34663532, 0.25125984, 0.15337386, 0.05395542, -0.04600213,
-0.14550003, -0.24354415, -0.33915486, -0.43137684, -0.51928865,
-0.6020119 , -0.67872005, -0.74864665, -0.81109301, -0.86543521,
-0.91113026, -0.9477216 , -0.97484362, -0.99222533, -0.99969304,
-0.99717216, -0.98468786, -0.96236488, -0.93042627, -0.88919115])
>>>
Pass a list in a function
It is also possible to pass a list:
>>> l = ['coucou','hello','salut']
>>> def fonction(l):
. for i in l:
. print i
.
>>> fonction(l)
coucou
hello
salut
References
How to pass an array or a list into a function in python ?
Examples of how to pass an array or list as an argument of a function in python:
Pass a matrix in a function
In python, it is possible to pass a matrix as an argument of a function, example:
>>> import numpy as np
>>> def function( x ):
. return 0.5 * x + 2
.
>>> x = np.arange(0,10,0.1)
>>> y = function(x)
>>> y
array([ 2. , 2.05, 2.1 , 2.15, 2.2 , 2.25, 2.3 , 2.35, 2.4 ,
2.45, 2.5 , 2.55, 2.6 , 2.65, 2.7 , 2.75, 2.8 , 2.85,
2.9 , 2.95, 3. , 3.05, 3.1 , 3.15, 3.2 , 3.25, 3.3 ,
3.35, 3.4 , 3.45, 3.5 , 3.55, 3.6 , 3.65, 3.7 , 3.75,
3.8 , 3.85, 3.9 , 3.95, 4. , 4.05, 4.1 , 4.15, 4.2 ,
4.25, 4.3 , 4.35, 4.4 , 4.45, 4.5 , 4.55, 4.6 , 4.65,
4.7 , 4.75, 4.8 , 4.85, 4.9 , 4.95, 5. , 5.05, 5.1 ,
5.15, 5.2 , 5.25, 5.3 , 5.35, 5.4 , 5.45, 5.5 , 5.55,
5.6 , 5.65, 5.7 , 5.75, 5.8 , 5.85, 5.9 , 5.95, 6. ,
6.05, 6.1 , 6.15, 6.2 , 6.25, 6.3 , 6.35, 6.4 , 6.45,
6.5 , 6.55, 6.6 , 6.65, 6.7 , 6.75, 6.8 , 6.85, 6.9 , 6.95])
x is a matrix and the function returns a new matrix y.
Note: it will return an error if a math function is used:
>>> import numpy as np
>>> import math
>>> def function( x ):
. return math.cos(x)
.
>>> y = function(x)
Traceback (most recent call last):
File "", line 1, in
File "", line 2, in function
TypeError: only length-1 arrays can be converted to Python scalars
since the math function do not work with matrix. It is necessary to use numpy.cos(x) instead:
>>> import numpy as np
>>> def function( x ):
. return np.cos(x)
.
>>> y = function(x)
>>> y
array([ 1. , 0.99500417, 0.98006658, 0.95533649, 0.92106099,
0.87758256, 0.82533561, 0.76484219, 0.69670671, 0.62160997,
0.54030231, 0.45359612, 0.36235775, 0.26749883, 0.16996714,
0.0707372 , -0.02919952, -0.12884449, -0.22720209, -0.32328957,
-0.41614684, -0.5048461 , -0.58850112, -0.66627602, -0.73739372,
-0.80114362, -0.85688875, -0.90407214, -0.94222234, -0.97095817,
-0.9899925 , -0.99913515, -0.99829478, -0.98747977, -0.96679819,
-0.93645669, -0.89675842, -0.84810003, -0.79096771, -0.7259323 ,
-0.65364362, -0.57482395, -0.49026082, -0.40079917, -0.30733287,
-0.2107958 , -0.11215253, -0.01238866, 0.08749898, 0.18651237,
0.28366219, 0.37797774, 0.46851667, 0.55437434, 0.63469288,
0.70866977, 0.77556588, 0.83471278, 0.88551952, 0.92747843,
0.96017029, 0.98326844, 0.9965421 , 0.99985864, 0.99318492,
0.97658763, 0.95023259, 0.91438315, 0.86939749, 0.8157251 ,
0.75390225, 0.68454667, 0.60835131, 0.52607752, 0.43854733,
0.34663532, 0.25125984, 0.15337386, 0.05395542, -0.04600213,
-0.14550003, -0.24354415, -0.33915486, -0.43137684, -0.51928865,
-0.6020119 , -0.67872005, -0.74864665, -0.81109301, -0.86543521,
-0.91113026, -0.9477216 , -0.97484362, -0.99222533, -0.99969304,
-0.99717216, -0.98468786, -0.96236488, -0.93042627, -0.88919115])
>>>
Pass a list in a function
It is also possible to pass a list:
>>> l = ['coucou','hello','salut']
>>> def fonction(l):
. for i in l:
. print i
.
>>> fonction(l)
coucou
hello
salut
References
Python Passing a List as an Argument
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
E.g. if you send a List as an argument, it will still be a List when it reaches the function:
Example
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
Related Pages
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Pass a List to a Function to act as Multiple Arguments
In this article, we will learn how to pass a list to a function to act as multiple arguments in Python. We will understand the basic approach with some custom codes. Let's first have a quick look over what is a list in Python.
Python List
Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values.
list1 = ["Ram", "Arun", "Kiran"] list2 = [16, 78, 32, 67] list3 = ["apple", "mango", 16, "cherry", 3.4]
Pass a List to a Function as Multiple Arguments
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. as function arguments during a function call. The function accepts them in the same format and returns the desired output. Now, we want to pass a list that contains multiple elements and these elements act as multiple arguments of a function. This concept of passing a single list of elements as multiple arguments is known as Unpacking Argument List. We use *args to unpack the single argument into multiple arguments. We use the unpacking operator * when arguments are not available separately.
For example, range() function in Python stores three different arguments - start, stop, and step. If the user does not want to input the values separately, he can write the function call with the * operator to unpack the arguments out of a list or tuple.
Example:
In this example, my_list is iterable that is passed as an argument. Function definition treats this list as multiple arguments. Python program loops over the given list and uses each element of the list as a separate argument to the function. The below code just unpacks the list using the *args syntax when defining the function.
#function definition def add(*params): sum = 0 for num in params: sum += num print(sum) #input list my_list = [1,2,3] #function call add(*my_list)
Example:
This method is useful when the elements are not predetermined. We can pass multiple elements to a python function without predetermining the formal parameters. This example uses *args to avoid the code failing when the user does not know how many arguments will be sent to the function. You can use this method when elements in a list are not predetermined.
def calculateTotalSum(*arguments): totalSum = 0 for number in arguments: totalSum += number print(totalSum) # function call calculateTotalSum(5,4,3,2,1)
Conclusion
In this article, we used different examples to understand how can we unpack the list elements to use them as multiple arguments. We discussed *args syntax in Python to unpack the arguments of the list and use them separately in the function body.
Python Passing a List as an Argument
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
E.g. if you send a List as an argument, it will still be a List when it reaches the function:
Example
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
Related Pages
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.