How do I copy dicts in Python with examples?

In Python, there are several ways to copy dictionaries. The most common methods include using the `copy()` method, the `dict()` constructor, and the dictionary comprehension technique. Each method has its own use cases depending on whether you need a shallow copy or a deep copy.

Example of Copying Dictionaries

# Original dictionary
original_dict = {'a': 1, 'b': 2, 'c': 3}

# Method 1: Using the copy() method
shallow_copy1 = original_dict.copy()

# Method 2: Using the dict() constructor
shallow_copy2 = dict(original_dict)

# Method 3: Using dictionary comprehension
shallow_copy3 = {key: value for key, value in original_dict.items()}

print(shallow_copy1)
print(shallow_copy2)
print(shallow_copy3)

Python copy dictionaries shallow copy deep copy dict methods