The fact that variables with the same value have the same id
is an artifact of the underlying implementation. For example, small integers are interned; as are common strings and those which appear as identifies. The advantage here is that - as strings and integers are immutable - every use can refer to the same memory location, reducing memory consumption.
However, not all "equal" values are "identical". The two lists as mentioned in your question are independent from one another. It is infeasible to detect if two objects happen to be equal, just in order to gain a small memory-saving, and having to separate them once they become un-equal.
It is a common source of confusion the see string comparisons as in foo is "bar"
instead of foo == "bar"
. It seems to work on CPython, if "bar"
has been interned. The comparison will always be false on other interpreters, though,