57
votes

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.

5
docs.python.org/tutorial/… briefly details to use the asterisk. It means you can pass an arbitrary amount of arguments, and they'll be wrapped up in a tuple (which can be accessed like a list within the function). An argument name with an asterisk has to come last so Python knows which is which. - JAL
"as Python tends to call them"? It's not a "tendency". It's a matter of definition. - S.Lott
S.Lott,calm down! I think you're being a bit harsh there - has that comment added anything to the conversation? no. - Jack Franklin
You are allowed to think it's harsh. However, word choice matters quite a bit, since other people, less knowledgeable will read this. I'm suggesting that "tends to call them" is a poor choice of words because it will confuse someone. There are enough Python questions with "array" instead of "list". This indicates folks aren't using the right terms and aren't succeeding in search existing documentation. There is enough confusion. It's a 'list'. Really. You can use that word, too, without fear of contradiction. - S.Lott
The funniest part... while I appreciate S.Lott's care with words, Python lists are actually implemented as dynamic arrays... lol. - anon01

5 Answers

77
votes

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.

28
votes

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.

8
votes

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
8
votes

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.

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

    return sum

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