Modifying a list inside a function

Question:

Suppose I have function with list parameter, and inside its body I want to modify passed list, by copying elements of an array to the list:

def function1 (list_arg):
   a = function2()    #function2 returns an array of numbers
   list_arg = list(a)

list1 = [0] * 5
function1(list1)
list1
[0,0,0,0,0]

When doing it like this, it doesn’t work. After executing function1(list1), list1 remains unchanged. So, how to make function1 return list1 with the same elements (numbers) as array a?

Asked By: roberto

||

Answers:

You can operate on the list to change its values (eg, append something to it, or set its values) but changes will be reflected outside of the function only if you operate on the reference to the passed in object:

def function1 (list_arg):
   list_arg.append(5)

If you have questions when doing this, print out the ids:

def function1 (list_arg):
   print 1, id(list_arg)
   list_arg[:] = ["a", "b", "c"]
   print 2, id(list_arg)
   list_arg = range(10)
   print 3, id(list_arg)

x = [1,2,3]
function1(x)
print x

prints:

1 4348413856
2 4348413856
3 4348411984
['a', 'b', 'c']

That is, x is changed in place, but assigning to the function’s local variable list_arg has no impact on x, because is then just assigns a different object to list_arg.

Answered By: tom10

You’re changing a reference to a local variable. When you pass in list_arg this way:

def function1 (list_arg):

list_arg is a reference to an underlying list object. When you do this:

   list_arg = list(a)

You’re changing what list_arg means within the function. Since the function exits right after that, list_arg = list(a) has no effect.

If you want to actually change the reference to the list you have to do assign it to the result of the function.

def function1 ():
   'a = some array'
   return list(a)

list1 = [0] * 5
list1 = function1()

Or you could modify the contents of the list without changing the reference.

def function1(list_arg):
    del list_arg[:]  # Clears the array
    'a = some array'
    list_arg.extend(a)
Answered By: Geoff Genz

If you assign something to the variable list_arg, it will from then on point to the new value. The value it pointed to before that assignment (your original list) will stay unchanged.

If you, instead, assign something to elements of that list, this will change the original list:

list_arg[:] = list(a)

This will make your code work as you wanted it.

But keep in mind that in-place changes are hard to understand and probably can confuse the next developer who has to maintain your code.

Answered By: Alfe

What I think you are asking is why after calling f(a), when f re-assigns the a you passed, a is still the “old” a you passed.

The reason for this is how Python treats variables and pass them to functions. They are passed by reference, but the reference is passed by value (meaning that a copy is created). This means that the reference you have inside f is actually a copy of the reference you passed. This again implies that if you reassign the variable inside the function. It is a local variable existing only inside the function; re-assigning it won’t change anything in outside scopes.

Now, if you rather than reassigning the local variable/reference inside f (which won’t work, since it’s a copy) perform mutable operations on it, such as append(), the list you pass will have changed after f is done.

See also the question How do I pass a variable by reference? which treats the problem and possible solutions in further detail.

TL;DR: Reassigning a variable inside a function won’t change the variable you passed as an argument outside the function. Performing mutable operations on the variable, however, will change it.

Answered By: Håvard S
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.