10. Modifying an element of a list in a function


#1



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


Oops, try again. list_function([0, 1]) returned 4 instead of [0, 4]


def list_function(x):
    return x[1] + 3
    return x

n = [3, 5, 7]
print list_function(n)


#2

a function ends the moment a return keyword is reached, so you first need to update the list, then return the whole list


#3

so update the list, do I add 3 to each number in the list??


#4

no, only the second element (first index) of the list


#5

def list_function(x):
return x[1] + 3
return x

n = [3, 8, 7]
return n
print list_function(n)

I am trying to return the whole list "return n" where did I go wrong?


#6

return is only used inside a function

you need to change this line:

return x[1] + 3

to update second element in list


#7

4 posts were split to a new topic: 10. modifying element of a list in a function


#8

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