Copying Dictionaries

You can make a copy of a dictionary using the copy() method or the dict() constructor.

Using copy() Method

Example: Copying a Dictionary

# Copying a dictionary using copy()
original = {"name": "Durai", "age": 28}
copy_dict = original.copy()
print("Copied Dictionary:", copy_dict)

Copied Dictionary: {'name': 'Durai', 'age': 28}

Using dict() Constructor

Example: Copying a Dictionary with dict()

# Copying a dictionary using dict()
copy_dict = dict(original)
print("Copied Dictionary:", copy_dict)

Copied Dictionary: {'name': 'Durai', 'age': 28}

Explanation: Both methods create a shallow copy of the dictionary. Changes to the copy do not affect the original dictionary.