List manipulation in functions


[I dont see why this is not accepted]( 

here my code:

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

print list_extender(n)

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

I think the error lies in the return of the list, but ive not been told that the list will be modified for testing otherwise im pretty sure my code would be fine?


Your function should return a value based on the argument given to it.

However, your function's return value is not at all affected by the content of the list that is given to it as an argument, you lost track of that list, you return another one.

This is a bit like having a function that adds two numbers, but only ever returns 157, regardless of what the two numbers are.


so would i return lst? sorry im a noob at this


ahhh i added n = lst in my function so that n can be changeable and that worked. thanks for giving me a different perspective :slight_smile:


Your function should only be concerned with the list that is given to it as an argument

n is an entirely different variable, the function shouldn't be mentioning it at all

Also, if you create a variable named n in your function, then that's a different variable from your global n - what you would have done is to have two variables refer to the same value which is fine and all but seems like it's dead code that can be removed


You want to return lst not n


i did try that but it didn't work, so i added

n = lst in my function and it worked.

would this be considered sloppy programming? i thought i was quite clever doing that, but maybe im just making my code unnecessarily long?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.