Modifying an element of a list in a function


#1

`Dear Sir,
find below error with question and my codes with error message. So, help me to find correct code.

project:
Change list_function so that:

Add 3 to the item at index one of the list.
Store the result back into index one.
Return the list.

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

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

error:

Oops, try again. list_function([4, 3]) returned 6 instead of [4, 6]

My regards,
TBIKU


#2

you need to return the whole list, not only the second item of the list

this means, you first need to update the second element in the list, and then on the line below return the whole list


#3

thank you i got with this below code:

def list_function(x):
x[1]=x[1]+3
return x
n = [3, 5, 7]
print list_function(n)


#4