Addressing the last element of a list

Daniel Crespo dcrespo at gmail.com
Thu Nov 10 11:16:08 EST 2005


Hi

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
proposition.

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

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 :)

Daniel




More information about the Python-list mailing list