70

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?

4
  • 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? Commented Feb 26, 2014 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. Commented Feb 26, 2014 at 22:26
  • 3
    Reading this may help your understanding, and not just of functions. Commented Feb 26, 2014 at 22:43
  • See also: stackoverflow.com/questions/10262920/… Commented Jan 2, 2022 at 2:22

4 Answers 4

93

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.

Sign up to request clarification or add additional context in comments.

7 Comments

What is the better more readable alternative? Return the new list and in the caller have a new_list = func_change_list(arr)?
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".
Note: list_arg[:] = a would also work; if you're able to call list on something, it's already an iterable, and somelist[:] = EXPR works so long as EXPR is iterable, so the list(a) is redundant.
@JoonhoPark: list_arg=function2() wouldn't modify the contents of the original list, it would replace/rebind the variable only within that function. list_arg[:] = function2() is what you need to modify the caller's list.
@ShadowRanger: Great!, RHS is already a new object. a=b will make 'a' point to a new object. But a[:] will change the elements of a.
|
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.

Comments

12

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.

2 Comments

Nice one! I'm still trying to figure out the question. :/
list_arg[:] = ["a", "b", "c"] is really helpful in a lot of scenarios !
3

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.