Addressing the last element of a list

Daniel Crespo dcrespo at
Thu Nov 10 17:16:08 CET 2005


Proposition 1:
> data = [0, None, 2, 0]
> ref = data[-1]
> ref = 1
> assert data[-1] == 1

Logically, it doesn't work. Here you are assigning to ref a NEW value.
That won't replace the data[-1] value. It's out of logic this

While this (proposition 2):
data = [0, None, 2, ["hello"]]
ref = data[-1]

does work, because you are assigning to ref THAT list ["hello"]. So, if
you have THAT list, and appends a value, THAT list will be modified.
ref is pointing to THAT object. So, I don't understand why you write
the first proposition, like if that can be a normal thinking of how to
do the second proposition :-S

Well, I hope that newcomers to Python don't confuse himselves :)


More information about the Python-list mailing list