Copy a Dictionary with Python

Copy a Dictionary

In Python, a dictionary is a collection of key-value pairs. Copying a dictionary can be necessary to avoid unintended modifications to the original dictionary or to work on modified versions in different contexts.

Methods for Copying a Dictionary

There are several ways to copy a dictionary in Python. Common methods include:

  • Using the copy() method
  • Using the dict() function
  • Using dictionary comprehension
  • Using the copy module

Using the copy() Method

The copy() method creates a shallow copy of the dictionary. A shallow copy means that the values in the dictionary are copied, but if these values are themselves mutable objects (like lists or dictionaries), only references to these objects are copied, not the objects themselves. 

original_dict = {'a': 1, 'b': 2, 'c': 3}
copied_dict = original_dict.copy()
print("Original Dictionary:", original_dict)
print("Copied Dictionary:", copied_dict)
# Modify the copy
copied_dict['a'] = 10
print("Modified Copied Dictionary:", copied_dict)
print("Original Dictionary after modification:", original_dict)
# Output:
# Original Dictionary: {'a': 1, 'b': 2, 'c': 3}
# Copied Dictionary: {'a': 1, 'b': 2, 'c': 3}
# Modified Copied Dictionary: {'a': 10, 'b': 2, 'c': 3}
# Original Dictionary after modification: {'a': 1, 'b': 2, 'c': 3}

Using the dict() Function

The dict() function can also be used to create a shallow copy of a dictionary. 

original_dict = {'x': 10, 'y': 20}
copied_dict = dict(original_dict)
print("Original Dictionary:", original_dict)
print("Copied Dictionary:", copied_dict)
# Modify the copy
copied_dict['x'] = 100
print("Modified Copied Dictionary:", copied_dict)
print("Original Dictionary after modification:", original_dict)
# Output:
# Original Dictionary: {'x': 10, 'y': 20}
# Copied Dictionary: {'x': 10, 'y': 20}
# Modified Copied Dictionary: {'x': 100, 'y': 20}
# Original Dictionary after modification: {'x': 10, 'y': 20}

Using Dictionary Comprehension

Dictionary comprehension can be used to create a shallow copy in an elegant way. 

original_dict = {'key1': 'value1', 'key2': 'value2'}
copied_dict = {k: v for k, v in original_dict.items()}
print("Original Dictionary:", original_dict)
print("Copied Dictionary:", copied_dict)
# Modify the copy
copied_dict['key1'] = 'new_value'
print("Modified Copied Dictionary:", copied_dict)
print("Original Dictionary after modification:", original_dict)
# Output:
# Original Dictionary: {'key1': 'value1', 'key2': 'value2'}
# Copied Dictionary: {'key1': 'value1', 'key2': 'value2'}
# Modified Copied Dictionary: {'key1': 'new_value', 'key2': 'value2'}
# Original Dictionary after modification: {'key1': 'value1', 'key2': 'value2'}

Using the copy Module

For cases where you need a deep copy, where nested objects should also be copied, you can use the copy module and its deepcopy() function. 

import copy
original_dict = {'key': [1, 2, 3]}
copied_dict = copy.deepcopy(original_dict)
print("Original Dictionary:", original_dict)
print("Copied Dictionary:", copied_dict)
# Modify the copy
copied_dict['key'].append(4)
print("Modified Copied Dictionary:", copied_dict)
print("Original Dictionary after modification:", original_dict)
# Output:
# Original Dictionary: {'key': [1, 2, 3]}
# Copied Dictionary: {'key': [1, 2, 3]}
# Modified Copied Dictionary: {'key': [1, 2, 3, 4]}
# Original Dictionary after modification: {'key': [1, 2, 3]}

Summary

  • copy(): Creates a shallow copy of the dictionary.
  • dict(): Another way to create a shallow copy.
  • Dictionary comprehension: An elegant method to create a shallow copy.
  • copy.deepcopy(): Creates a deep copy, necessary if the dictionary contains nested objects.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print