11/18 Why lst.append(9) and not lst = lst.append(9)?


#1

I am no longer stuck but I have a question about this exercise:

https://www.codecademy.com/en/courses/python-beginner-nzzVa/2/4?curriculum_id=4f89dab3d788890003000096

Because we are modifying a list inside of a function, I set the function to re-assign the argument lst to lst.append(9), and then return the newly-assigned lst like so:


n = [3, 5, 7]
def list_extender(lst) :
    lst = lst.append(9)
    return lst
print list_extender(n)

Turns out that this does not work and nets me a 'list_extender([1, 2, 3, 4]) returned None, did you remember to return the result?' error message.

This is what works:

n = [3, 5, 7]
def list_extender(lst) :
    lst.append(9)
    return lst
print list_extender(n)

Why does the first function not work? Is it because .append() is actually a function itself and I put in an unnecessary step by re-assigning the lst variable? Am I wrong to treat lst like a variable when defining a function that takes it as an argument? I feel like I am making a conceptual error, and would like to understand what it is.

Sorry for stupid question, am new at this.


#2

Hi Anna, You have asked a good question! I will point you to the docs to understand this problem. Focus on the heading - "return value".

If you are still unable to understand after viewing the doc, reply and I'll further explain :slight_smile:


#3

Hi, @boardrunner72218! Your question, as @gaurangtandon mentioned, is a very good one! This is how I understand it:
When you say lst.append(9), your basically issuing a command, saying "Add 9 to 'lst'". So your code is saying, "When I apply the function 'list_extender' on a list, I want it to append the value 9 to the list and give me the modified list (the list with a 9)." By saying 'lst = lst.append(9)', you might think you're saying 'the list is now modified', but you're actually treating the list like a function!
Hope this helps!
Have fun!


#4

@hrithiktheboss,

You could also play around with
hex(id(variable-name))
to see to which memory-address the variable is pointing...

n = [3, 5, 7]
print hex(id(n))
def list_extender(lst) :
    print(lst)
    print hex(id(lst))
    lst = (lst.append(9))
    print hex(id(lst))
    #lst = list
    print(lst)
    print hex(id(lst))
    #return lst
lst_outside=list_extender(n)
print(lst_outside)
print hex(id(lst_outside))
print n

print("=======================")
def list_extender1(liist) :
    print(liist)
    print hex(id(liist))
    #lst = lst.append(9)
    liist.append(9)
    print hex(id(liist))
    #lst = list
    print(liist)
    print("====")
    return liist

#5

gaurangtandon: Thank you!! This certainly explains the 'return none' error. I see now what I did.


#6

hrithiktheboss: And thank you as well for addressing my conceptual question! I was putting .append() into the wrong 'category of things' in my head.


#7

@boardrunner72218 No problem!! :slight_smile:


#8

Thanks, @leonhard.wettengmx.n! That's a nice function, I'll surely look into it.