Suppose I have function with list parameter, and inside it's body I want to modify passed list, by this code:
spy = [0,0,7]
def replace_spy(lista):
return lista[2]=lista[2]+1
but it show me the error : SyntaxError: invalid syntax
Suppose I have function with list parameter, and inside it's body I want to modify passed list, by this code:
spy = [0,0,7]
def replace_spy(lista):
return lista[2]=lista[2]+1
but it show me the error : SyntaxError: invalid syntax
Assignment is a statement, not an expression. It has no value. So you can't return
the value of an assignment.
Across languages that do allow this kind of thing, it's ambiguous what exactly the return value should be, but most of them—including C and the many languages derived from or inspired by C—would give you the new value of lista[2]
1. So presumably you want this:
def replace_spy(lista):
lista[2]=lista[2]+1
return lista[2]
If you're looking to shorten things, this is fewer keystrokes, and probably more readable:
def replace_spy(lista):
lista[2] += 1
return lista[2]
1. As an "lvalue", but that isn't a meaningful thing in Python.