This question is just out of general curiosity. I’ve just noticed it when working on my current project (surprisingly I haven’t came across before today).
Take this code:
List = ["hi","stack","over","flow","how","you","doing"]
del List(len(List)-1)
Error:
SyntaxError: can't delete function call
I don’t understand why you aren’t allowed to delete an index of a list by referencing a call to a function? Do I just shut up and accept you can’t do it or am I doing something fundamentally wrong?
I apologise if there is an easy answer to this but either Google is getting less helpful or this is so blatantly obvious I need help.
You meant to delete the last element of the list, not somehow call
Listas a function:Python’s
delstatement must take specific forms like deleting a variable, list[element], or object.property. These forms can be nested deeply, but must be followed. It parallels the assignment statement — you’ll get a similar syntax error if you try to assign to a function call.Of course, what you really want in this case is
which means the last element of the list, and is way more Pythonic.