Question to return in 11 - List manipulation in functions


#1

Hello there!

I have a question regarding list manipulation in functions.

The code says:

def list_extender(lst):
    lst.append(9)
    return lst

So far everything is understandable and the result as expected.

However, if I change the code to

def list_extender(lst):
    lst.append(9)
    return n

I get a strange result:

I'd have expected

print list_extender(n)

to return [3 ,5, 7] , but instead it returns [3 ,5, 7, 9].

Does that mean, that although something else is fed into the function ( [1, 2, 3, 4] in this case) not the actually provided list gets changed, but always the list defined in the script?


#2

Hi @specialsymbol ,

When an object, such as a list, is passed to a function as an argument, then the function parameter, which in this case is lst, refers to the very same object that was passed as an argument, and not merely a copy of it. So, if the function alters the object referred to by the parameter, it alters the original object.

Here is a version that alters a copy, instead ...

n = [3, 5, 7]
# Add your function here
def list_extender(lst):
    lst_copy = list(lst)
    lst_copy.append(9)
    return lst_copy

print list_extender(n)
print n

Output ...

[3, 5, 7, 9]
[3, 5, 7]

#3

Hello, thanks for the reply!

Ok, so far so good.
There is only one thing (I have failed to mention this in my first post) I don't understand:

First, a list is created in said script:

n = [3, 5, 7]

Then the function above is defined.

But, and this is interesting, to test my function codecademy feeds it something else than my list

n

, it feeds it directly the list

[1, 2, 3, 4]

.

This is handed to said function (list_extender) as an argument. Hence, codecademy would expect the output

[1, 2, 3, 4, 9]

. But since I didn't return

lst

but

n

, it doesn't return the manually fed

[1, 2, 3, 4]

with an appended 9, but - the list

n = [3, 5, 7]

And, now the part I don't understand, this list appended by 9 although n is only mentioned in return of the script, and not altered by it. It is also not provided as the argument of the function, as the argument is the directly provided list [1, 2, 3, 4].


#4

Hi @specialsymbol ,

A feature of Codecademy that sometimes causes confusion is that during a coding session in which you repeatedly edit and submit solutions for the same exercise, values of variables are not discarded between submissions. Related to this is that in this exercise, when Codecademy calls your function to test it, the system remembers the variable, n, even though the initial assignment statement is not executed when Codecademy calls your function and passes it the list, [1, 2, 3, 4].

Let's assume that this is your code ....

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

print list_extender(n)

When you submit it, n is initialized outside your function, and then your function appends the 9 to it. So n now refers to the list [3, 5, 7, 9].

Then, Codecademy calls your function, and it returns the list referenced by n that is still in memory. That list is [3, 5, 7, 9].

With the above in mind, try the following ...

Submit this code ...

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

print list_extender(n)

Then, comment out the assignment statement for n, so that you have this ...

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

print list_extender(n)

Because n has been assigned a value during this edit session, it still exists.

Now, submit your edited code again a few times. Every time you submit your code, another 9 is appended to n, and every time Codecademy tests your function, the function returns n. So, don't be surprised when you eventually see something like this ..

Oops, try again. list_extender([1, 2, 3, 4]) returned [3, 5, 7, 9, 9, 9, 9, 9, 9, 9] instead of [1, 2, 3, 4, 9]


#5

mine printed [3,5,7,9]