How to copy a dictionary and only edit the copy
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When working with Python dictionaries, there are times when you might want to create a copy of a dictionary and modify only the copy without affecting the original. This can be crucial when you want to preserve the original data structure while performing alterations, experiments, or what-if analyses on the copy. This article details methods to copy a dictionary in Python and discusses why different techniques might be used under different circumstances.
Copying a Dictionary
In Python, dictionaries are mutable objects, meaning their contents can be changed. However, Python provides multiple ways to create a copy of a dictionary so you can modify it independently.
Method 1: Using the copy() Method
The simplest way to create a shallow copy of a dictionary is by using the copy() method. This creates a new dictionary object with the same key-value pairs as the original.
Note: The copy() method performs a shallow copy, which means if your dictionary contains nested objects (like other dictionaries), those nested objects will not be deeply copied.
Method 2: Using the dict() Function
Another straightforward way to clone a dictionary is by using the dict() function:
Like the copy() method, dict() also yields a shallow copy.
Method 3: Using Dictionary Comprehension
A more manual method is using dictionary comprehension. This allows for more control over what gets copied, and you can even transform the data during the copy process.
Method 4: Using the deepcopy() Function
For complex nested dictionaries and ensuring every nested object is independently copied, use deepcopy() from the copy module:
With deepcopy(), even the nested dictionaries are copied independently, which is not the case with shallow copies.
Shallow vs Deep Copies
Shallow Copy: Only copies the references of nested objects, meaning changes to nested objects in the copy affect the original.
Deep Copy: Copies every level of the dictionary deeply. Nested dictionaries and other mutable objects are duplicated, so changes do not affect the original.
Key Points to Consider
- Use Cases:
- Default to using
copy()ordict()for simple, single-layer dictionaries. - Use
deepcopy()for dictionaries with nested structures you want to isolate.
- Nested Structures:
- If you're unsure about the levels of nested objects, default to
deepcopy().
- Performance:
copy()anddict()are faster thandeepcopy(), use them when performance is a concern and deep copying isn’t necessary.
Summary
Below is a tabular summary highlighting the various methods:
| Method | Description | Shallow/Deep | Performance |
copy() | Method of dict object | Shallow | Fast |
dict() | Built-in Python function | Shallow | Fast |
| Comprehension | Custom implementation | Shallow | Depends on logic |
deepcopy() | copy module function | Deep | Slower |
Conclusion
Knowing how and when to copy a dictionary in Python, and understanding the implications of shallow vs. deep copies, significantly improves one's ability to perform operations on complex data structures safely. Choose the method that suits your use case best, balancing between performance and functionality as needed.
Related reading
- How to copy a java.util.List into another java.util.List
- How to copy messages to another queue on RabbitMQ?
- How to correctly save instance state of Fragments in back stack?
- How to correctly shut down Python RQ worker processes dynamically?
- how to copy s3 object from one bucket to another using python boto3
- How to correct TypeError Unicode-objects must be encoded before hashing?
- How to count groups of same cells in a 2d array?
- How to count occurrences of an element in a Swift array?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.