Yes, this is intended behaviour. Python names and entries in dictionaries and lists are mere references to the actual objects, stored in a big pile (the heap) in memory.
Thus, my_dict[2]
and chili
both refer to the same list
object, and list
objects are mutable. Deleting an entry from a list
object means that all references to that object see the change.
If you want chili
to not be the same list object, you must create a copy. You can create a shallow copy with either:
chili = my_dict[2][:]
as slicing from first to last index produces a new list object, or using:
chili = list(my_dict[2])
which produces a new list
object, copying all references stored in the original sequence.
These create shallow copies; if anything in my_dict[2]
is itself mutable you would still be manipulating an object shared between the chili
list and the my_dict[2]
list.
You can create a deep copy by using the copy.deepcopy()
function, which recursively produces copies of objects.