Jack Franklin
Jack Franklin

Reputation: 3765

Passing an array/list into a Python function

I've been looking at passing arrays, or lists, as Python tends to call them, into a function.

I read something about using *args, such as:

def someFunc(*args)
    for x in args
        print x

But not sure if this is right/wrong. Nothing seems to work as I want. I'm used to be able to pass arrays into PHP function with ease and this is confusing me. It also seems I can't do this:

def someFunc(*args, someString)

As it throws up an error.

I think I've just got myself completely confused and looking for someone to clear it up for me.

Upvotes: 66

Views: 433642

Answers (6)

VojtaK
VojtaK

Reputation: 678

There is a way how to pass list directly instead of function params. In this scenario you have function with fixed number of parameters, would like to pack these into the list and call the function using your list.

The function scipy.optimize.curve_fit utilizes this as it expect the function parametrized by apriori unknown number of values, e.g. you can specify

import numpy as np
def f(x, a0, a1, a2):
   return a0+a1*np.exp(a2*x)

To have a function parametrized by three parameters. Then you call

fit, cov = curve_fit(f, xdata, ydata)

and the function returns fit list of the length corresponding to the number of parameters, interestingly you can then obtain value v of function parametrized by fit params at the point x by calling

fit=[1,2,3]
x=0
v = f(x, *fit)

See scipy sources

Upvotes: 1

ishaj
ishaj

Reputation: 101

def sumlist(items=[]):
    sum = 0
    for i in items:
        sum += i

    return sum

t=sumlist([2,4,8,1])
print(t)

Upvotes: 2

g.d.d.c
g.d.d.c

Reputation: 47978

When you define your function using this syntax:

def someFunc(*args):
    for x in args
        print x

You're telling it that you expect a variable number of arguments. If you want to pass in a List (Array from other languages) you'd do something like this:

def someFunc(myList = [], *args):
    for x in myList:
        print x

Then you can call it with this:

items = [1,2,3,4,5]

someFunc(items)

You need to define named arguments before variable arguments, and variable arguments before keyword arguments. You can also have this:

def someFunc(arg1, arg2, arg3, *args, **kwargs):
    for x in args
        print x

Which requires at least three arguments, and supports variable numbers of other arguments and keyword arguments.

Upvotes: 96

JoshD
JoshD

Reputation: 12824

You can pass lists just like other types:

l = [1,2,3]

def stuff(a):
   for x in a:
      print a


stuff(l)

This prints the list l. Keep in mind lists are passed as references not as a deep copy.

Upvotes: 37

Gintautas Miliauskas
Gintautas Miliauskas

Reputation: 7892

Python lists (which are not just arrays because their size can be changed on the fly) are normal Python objects and can be passed in to functions as any variable. The * syntax is used for unpacking lists, which is probably not something you want to do now.

Upvotes: 8

JAL
JAL

Reputation: 21563

You don't need to use the asterisk to accept a list.

Simply give the argument a name in the definition, and pass in a list like

def takes_list(a_list):
    for item in a_list:
         print item

Upvotes: 8

Related Questions