If we make changes to a list passed in as an argument, does it change the list itself?

Question

If we make changes to a list passed in as an argument, does it change the list itself?

Answer

Without going into too much detail, the general answer is yes, changes to a list that is passed in as an argument for a function can change the list itself.

Say for example, that you had the following function that takes in a list, and changes the value at index 1 to the value 20.

def update_list(lst):
  lst[1] = 20

When we run this function with any list as an argument, it will change the value being referenced for that list at index 1.

numbers = [1, 2, 3, 4]
update_list(numbers)
print(numbers) # [1, 20, 3, 4]

names = ["Alice", "Bob", "Carl"]
update_list(names)
print(names) # ["Alice", 20, "Carl"]

The reason for this is that Python lists store references to objects, rather than the objects themselves. So, running this function is changing the reference at index 1 of the lists to be changed to the value 20.

10 Likes

Another way to describe a list is as a pointer object. As described above, each element in the list is a reference to an object in memory somewhere. The objects are not passed into the function but remain where they are. If we manipulate an element, it will simply point to a new object but remain referenced from the same index in the list as before.

7 Likes

Clarifying, I do not speak English well.

So, if I want to test things or validate with some changes in the list, the best solution would be to save this list and the changes in a temporary list within the function, right?

It all depends upon what you want the global list to look like when your function is completed. If you want it to stay the same, then make a copy inside the function and return a new list. If you want it changed, then don’t make a copy. Regardless what you call the list inside the function, the global object will see any changes performed by the function.

2 Likes

i do not understand the iintent behind this question,can someone tell me what was another thing that could occur?

The title of this thread is: If we make changes to a list passed in as an argument, does it change the list itself?

Let us say that we need a function that will return a given list with the character ‘x’ appended.

Version 1 (answer to question: yes):

def add_x(lst):    
    lst.append('x')
    return lst
    
my_lst = ['a', 'b', 'c']
new_lst = add_x(my_lst)

print("new_lst is {}".format(new_lst))
print("my_lst is {}".format(my_lst))

#Output:
new_lst is ['a', 'b', 'c', 'x']
my_lst is ['a', 'b', 'c', 'x']    # original list  is changed

Version 2 (answer to question: no):

def add_x(lst):
    out_lst = lst[:]
    out_lst.append('x')
    return out_lst
    
my_lst = ['a', 'b', 'c']
new_lst = add_x(my_lst)

print("new_lst is {}".format(new_lst))
print("my_lst is {}".format(my_lst))

#Output:
new_lst is ['a', 'b', 'c', 'x']
my_lst is ['a', 'b', 'c']     # original list is not changed
4 Likes

You made it so much clear,
Thanks a lot

1 Like