64

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
roberto
  • 679
  • 1
  • 6
  • 11
  • So, what exactly do you expect the `list1` to contain when your function returns? What is the content of `a` and where is it defined? – msvalkon Feb 26 '14 at 22:24
  • 1
    @user155 FYI, the standard syntax for comments is `#Your comment here`. Using strings is usually reserved for when you want multiline comments or docstrings. – Asad Saeeduddin Feb 26 '14 at 22:26
  • 3
    Reading [this](http://nedbatchelder.com/text/names.html) may help your understanding, and not just of functions. – John Y Feb 26 '14 at 22:43
  • See also: https://stackoverflow.com/questions/10262920/understanding-pythons-call-by-object-style-of-passing-function-arguments – Karl Knechtel Jan 02 '22 at 02:22

4 Answers4

81

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.

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • What is the better more readable alternative? Return the new list and in the caller have a `new_list = func_change_list(arr)`? – mLstudent33 Apr 19 '21 at 03:32
  • 3
    Given that returning a new (slightly changed) list is really a very different action (takes more memory and time, depending on the list size), one cannot really compare the two and say one is better than the other. If the program truly needs a changing of the list, its name should clearly reflect this. A typical phrase to reflect this would be "in_place". – Alfe Apr 19 '21 at 13:25
  • Going off Alfe's comment, as opposed to adding that to the name of the function which could be making an already verbose function name more so, You could add an optional "in_place" paramater, then write your function such that it can mimic either behavior – Bud Linville May 26 '22 at 13:02
12

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.

Community
  • 1
  • 1
Håvard S
  • 23,244
  • 8
  • 61
  • 72
8

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.

tom10
  • 67,082
  • 10
  • 127
  • 137
2

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)
Geoff Genz
  • 2,006
  • 17
  • 20