Modifying an element of a list in a function


#1



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

This is the error i get:


File "python", line 2
return x[1] = x[1] + 3

SyntaxError: invalid syntax

Can someone help with this issue please?

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

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


#2

you can't assign value in return
first assign, and in other line return
this is correct:

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

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

#3

Hi
you shouldn't modify the value of x[1] inside the return but before it, so your code should look like

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

So the rule is : no assignment inside the return.

PS it is however allowed to have boolean operators inside the return, for example

def list_comparison(x):
    return x[1] == x[2]

this function will return True if x[1] = x[2] and False otherwise.


#4

Thanks!! @thebelgian1