How to pass an array or a list into a function in python ?

Published: June 13, 2019

DMCA.com Protection Status

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 "<stdin>", line 1, in <module>
   File "<stdin>", 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