Why can a list be changed without being reassigned?

What I am wondering about the solution to the code is the following:
in every line we append(), how is the result (“refreshed” list) stored without using “return” or without assigning it to a new variable? To be more specific:

def append_sum(lst):
lst.append(lst[-1] + lst[-2]) # 3
the first “refreshed” list is now [1, 1, 2, 3]
then, we write again
lst.append(lst[-1] + lst[-2]) #5
the second “refreshed” list is now [1, 1, 2, 3, 5]

In what way, was the first “refreshed” list saved? We did not name it with a new variable nor we returned it. As we did not do something of the previous, I’d expect the second “refreshed” list to be [1, 1, 2, 3, 3]. Obviously, my above question applies to the the process that is followed for the third “refreshed” list as well.

Is the answer related to the way “append” is used? Is by definition the use of append changes for ever the items containing in the original list? And if this is the case, what happens if we want to append something in a list (even append more than once), work with the new items of the list but afterwards we need again to work with the original items?

3 Likes

In Python, lists are mutable objects, meaning that they can be changed. If you make the assignment
var = 'abc'
… then var will be bound to the value ‘abc’ forever, unless you make a new assignment,
var = 'abcd'
Calling var = var + 'd' , for instance, is a new assignment. It does not change the original string, but rather assigns a new string, ‘abcd’ to the variable var. Strings in Python are immutable.

But if you make the assignment lst = [1,2,3], you can do things to that list, such as append to it or change its elements without a new assignment. The list object is mutable.

Your final question is very important. If you will need access to your original list, then all of your manipulation (append, etc.) should be on a completely separate copy (not an alias) of the list. I’m not sure if your course has thus far gotten into the details of copying, but here’s an introduction.

8 Likes

Great answer Patrick, I was kind of wondering the same thing and this post helped me understand this in a much clear way.

1 Like

Every time we use the append function on the list, the list will be auto saved in-place, which there is no such other new list but only one itself :slight_smile:
ex:
list = [1, 2, 3]
list.append(4) # [1, 2, 3, 4]
Then every time we do the addition between the last element and the pre-last one and use append function to save it into the given list , the given list , the original one will not create any new list at all.
It is how append function works.

Can we make a nicer code from this:

def append_sum(lst):
lst.append(lst[-1] + lst[-2])
lst.append(lst[-1] + lst[-2])
lst.append(lst[-1] + lst[-2])
return lst
because i feel that isn’t the good way to write code. maybe some kind of for loop?
i have tried and I can’t make it work

If you have three identical lines then sure put it in a loop that iterates 3 times, you wouldn’t need to change anything.

But if something specifically does something 3 times then a loop isn’t an improvement I’d rather look at three identical lines.

But you probably wouldn’t actually ever need a function like this - it’s oddly specific

If you wanted to implement the fibonacci sequence (isn’t that’s what’s going on there…) you might do this instead:

def fib():
    a, b = 0, 1
    while True:
        yield a
        a, b = (b, a + b)

Or maybe you’d populate a list of some particular size instead.