How do I deep copy dicts in Python safely and idiomatically?

In Python, a deep copy of a dictionary can be performed safely and idiomatically using the `copy` module, specifically the `deepcopy` function. This allows you to create a new dictionary that is a deep copy of the original, ensuring that nested objects are also copied, rather than just referenced.

import copy

original_dict = {
    'key1': [1, 2, 3],
    'key2': {'subkey1': 'value1', 'subkey2': 'value2'}
}

# Create a deep copy of the original dictionary
deep_copied_dict = copy.deepcopy(original_dict)

# Modify the deep copied dictionary
deep_copied_dict['key1'][0] = 100
deep_copied_dict['key2']['subkey1'] = 'modified_value'

print("Original Dictionary:", original_dict)
print("Deep Copied Dictionary:", deep_copied_dict)

Python deep copy dictionary copy module deepcopy