Results.

SCEPTRA.TECH

Python dict.copy Still Changes the Original? Follow the Nested Mutation

Original and shallow-copied dictionaries can share the same nested tags list

You copy a settings dictionary, edit a nested list in the copy, and the original settings change too. The copy may be working exactly as documented. dict.copy() makes a new outer dictionary, but it does not recursively copy the objects stored inside it. For nested mutable data, outer separation is not enough.

Check the nested object's identity

original = {
    'name': 'base',
    'filters': {'tags': ['open']},
}
draft = original.copy()

print(draft is original)  # False
print(draft['filters'] is original['filters'])  # True

draft['name'] = 'draft'
draft['filters']['tags'].append('urgent')
print(original)
# {'name': 'base', 'filters': {'tags': ['open', 'urgent']}}

Assigning a new value to draft['name'] changes only the new dictionary's entry. Appending to the tags list changes an object both paths can still reach. The is operator tests object identity, so it helps locate this sharing without relying only on values that happen to look equal.

Plain assignment is a different case: draft = original gives the same dictionary another name. Neither assignment nor a shallow copy promises an isolated snapshot of an entire object graph.

For a small known schema, rebuild the path you change

If the intended edit is adding a tag while keeping the original settings, construct fresh containers along that path:

original = {
    'name': 'base',
    'filters': {'tags': ['open']},
}
draft = {
    **original,
    'filters': {
        **original['filters'],
        'tags': [*original['filters']['tags'], 'urgent'],
    },
}
print(original['filters']['tags'])  # ['open']
print(draft['filters']['tags'])     # ['open', 'urgent']

This example creates a new outer dictionary, a new filters dictionary and a new tags list. It assumes the shown keys and types exist. It is not schema validation and not a deep copy of every field. Other nested branches stay shared unless you deliberately rebuild them too.

I would use this explicit form for a small, stable settings shape because the intended change is visible. Review the actual mutation path: copying the filters dictionary but reusing its tags list would still leave that list shared. If list elements are themselves mutable objects, copying the list alone does not isolate those elements.

Use deepcopy when recursive copying fits the data

from copy import deepcopy

original = {'filters': {'tags': ['open']}}
draft = deepcopy(original)
draft['filters']['tags'].append('urgent')
print(original['filters']['tags'])  # ['open']

For this plain dictionary-and-list fixture, deepcopy creates the needed separation. It is not a universal switch for safe duplication. Classes can customize copying, and objects such as files and sockets are not duplicated by the copy module. Deep copying may also copy more than intended when some state should remain shared.

Keep the decision tied to ownership. A configuration made of simple data is different from a live client object that holds a connection. For that kind of object, use its documented construction or lifecycle rather than treating a recursive copy as a new independent session.

Test the mutation you actually need

I ran these synthetic examples in Python 3.10.12. The shallow copy shared its filters dictionary and changed the original tags. Explicit reconstruction created separate filters and tags containers, while deepcopy kept the original tags unchanged. No production configuration, custom class or memory benchmark was tested.

A useful regression test performs the intended edit and then asserts that the original nested values are unchanged. An assertion that the two outer dictionaries are different objects cannot catch a shared nested list. Test the boundary you need, not only the copy method's name.

Sources

Comments