Python – Copy Dictionaries

In Python, dictionaries are mutable objects, and sometimes you may need to create a copy of a dictionary. There are different methods to copy a dictionary depending on whether you want a shallow copy (a new dictionary with references to the original objects) or a deep copy (a completely independent copy).

Here are the methods to copy dictionaries:


1. Shallow Copy using copy() Method

The copy() method creates a shallow copy of the dictionary. This means the new dictionary will have the same keys and values as the original dictionary, but if any values are mutable objects (like lists or other dictionaries), they will still reference the same objects as the original dictionary.

Example:

original_dict = {'a': 10, 'b': 20, 'c': [30, 40]}

# Creating a shallow copy
copied_dict = original_dict.copy()

# Modifying the original dictionary
original_dict['a'] = 100
original_dict['c'].append(50)

print("Original Dictionary:", original_dict)  # Output: {'a': 100, 'b': 20, 'c': [30, 40, 50]}
print("Copied Dictionary:", copied_dict)  # Output: {'a': 10, 'b': 20, 'c': [30, 40, 50]}

As you can see, the value associated with the key 'c' is a list, and because it’s mutable, the list is shared between the original and the copied dictionary.


2. Shallow Copy using dict() Constructor

You can also create a shallow copy using the dict() constructor, which takes an existing dictionary as input.

Example:

original_dict = {'a': 10, 'b': 20, 'c': 30}

# Creating a shallow copy using dict()
copied_dict = dict(original_dict)

print("Original Dictionary:", original_dict)  # Output: {'a': 10, 'b': 20, 'c': 30}
print("Copied Dictionary:", copied_dict)  # Output: {'a': 10, 'b': 20, 'c': 30}

3. Deep Copy using copy Module

A deep copy creates a new dictionary and also recursively copies all nested objects, so the new dictionary is completely independent of the original.

To create a deep copy, you need to use the deepcopy() function from the copy module.

Example:

import copy

original_dict = {'a': 10, 'b': 20, 'c': [30, 40]}

# Creating a deep copy
copied_dict = copy.deepcopy(original_dict)

# Modifying the original dictionary
original_dict['a'] = 100
original_dict['c'].append(50)

print("Original Dictionary:", original_dict)  # Output: {'a': 100, 'b': 20, 'c': [30, 40, 50]}
print("Copied Dictionary:", copied_dict)  # Output: {'a': 10, 'b': 20, 'c': [30, 40]}

In this case, the change to the list inside the original dictionary does not affect the copied dictionary, because the deep copy creates a completely independent copy of all nested objects.


4. Copying Using Dictionary Comprehension

You can also use dictionary comprehension to create a copy of a dictionary.

Example:

original_dict = {'a': 10, 'b': 20, 'c': 30}

# Creating a copy using dictionary comprehension
copied_dict = {key: value for key, value in original_dict.items()}

print("Original Dictionary:", original_dict)  # Output: {'a': 10, 'b': 20, 'c': 30}
print("Copied Dictionary:", copied_dict)  # Output: {'a': 10, 'b': 20, 'c': 30}

5. Copying Using update()

The update() method can be used to copy the contents of one dictionary into another. However, this method modifies the target dictionary in place, so you need to make sure the target dictionary is empty before using it as a copy.

Example:

original_dict = {'a': 10, 'b': 20, 'c': 30}

# Creating an empty dictionary and copying the original dictionary into it
copied_dict = {}
copied_dict.update(original_dict)

print("Original Dictionary:", original_dict)  # Output: {'a': 10, 'b': 20, 'c': 30}
print("Copied Dictionary:", copied_dict)  # Output: {'a': 10, 'b': 20, 'c': 30}

Summary of Copy Methods:

  • Shallow Copy: copy(), dict(), or using dictionary comprehension.
  • Deep Copy: copy.deepcopy() from the copy module (for nested or complex objects).
  • update(): Copies the contents of one dictionary into another (modifies the target dictionary).
Leave a Reply 0

Your email address will not be published. Required fields are marked *