Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+5 votes

In this question, 4 functions are defined, which take a list as an input, and do NOT return anything, which I thought meant that the list which is input to them could not be changed by them, since they only change the list locally. This works for the first 2 functions, but the second 2 functions, which look very similar, change theList. Why? how can changing myLIst within a function change theList without using return?

asked in CSC201 Spring 2021 by (1 point)

3 Answers

+3 votes
 
Best answer

Yes, there is an important distinction between re-assigning a variable (which causes it to point to a different object), and modifying the object that a variable refers to.

Since lists are mutable, it's possible to modify the list object. e.g.

myList[0] = "new item"

If myList was a parameter in a function, then whatever list was passed in for myList will also be modified, because myList is referring to that same object, and we are modifying that object.

However, if you do:

myList = ["whole", "new", "list"]

then you are NOT modifying the original list object, you are just changing myList to point to a new list object that you created. Re-assigning the variable (or parameter) myList in one function will NOT affect the value of the list object that was passed in.

I realize this is a rather subtle distinction, but it is an important one, about how Python variables and assignment statements really work.

If we ignore functions and parameters for a moment, we can see the same issue comes up just using assignment statements:

x = [1, 2, 3]
y = x
y[0] = 100   #changes the first element of the list that both x and y refer to
print(f"x={x}  y={y}")

y = [4,5,6]  #changes y to point to a new/different list object
             # but does NOT change the list that x refers to (and y used to refer to)
print(f"x={x}  y={y}")

Now, since actual parameters are passed to functions "by assignment", the similar issue happens here:

def changeItem(y):
     y[0] = 100

def tryToChangeWholeList(y):
     y = [4,5,6]

def main():
    x = [1,2,3]
    changeItem(x)
    print(x)
    tryToChangeWholeList(x)
    print(x)
answered by (508 points)
selected by
+3 votes

I was confused by this as well, but as I understand it, it has something to do with the fact that the individual values in the list were being redefined new values added, rather than redefining the list as a whole. I'm not 100% sure, but that's what I found to be different.

answered by (1 point)
+2 votes

You could also write an if statement with repeating elif statements to produce a different output for each input.

answered by (1 point)
...