How do I deep copy dicts in Python with standard library only?

In Python, to create a deep copy of dictionaries using the standard library, you can utilize the `copy` module, which provides the `deepcopy` function. This function recursively copies the original dictionary, ensuring that all nested objects are also copied rather than just their references. This is particularly useful when dealing with complex data structures that include nested dictionaries, lists, or other mutable types.

Here’s an example of how to use the `deepcopy` function:

import copy # Original dictionary original_dict = { 'a': 1, 'b': [2, 3], 'c': {'d': 4} } # Deep copy of the original dictionary copied_dict = copy.deepcopy(original_dict) # Modifying the copied dictionary copied_dict['b'][0] = 99 copied_dict['c']['d'] = 100 print("Original Dictionary:", original_dict) print("Copied Dictionary:", copied_dict)

deep copy dictionary copy Python copy module