Python Dictionary copy() Method
dict.copy() creates a new dictionary with the same key-value pairs as the original.
This method is useful when you want a separate dictionary object instead of another variable pointing to the same dictionary. It is important to know that copy() makes a shallow copy, not a deep copy.
Quick example #
student = {"name": "Ana", "age": 20}
student_copy = student.copy()
print(student_copy)
print(student_copy is student)
Output:
{'name': 'Ana', 'age': 20}
False
Use copy() to create a new dictionary object with the same contents. The result is a shallow copy, not a deep copy.
What the copy() method does #
dict.copy()returns a new dictionary- The new dictionary starts with the same keys and values as the original
- The original dictionary is not changed by calling
copy() - It is useful when you want a separate dictionary object
If you are still learning how dictionaries work, see Python dictionaries explained or creating a dictionary in Python.
Basic syntax #
new_dict = old_dict.copy()
Key points:
copy()takes no arguments- It returns the copied dictionary
- You should store the result in a variable if you want to use the new dictionary
Example:
colors = {"a": "red", "b": "blue"}
new_colors = colors.copy()
print(new_colors)
Output:
{'a': 'red', 'b': 'blue'}
Simple example #
Here is a basic example with simple values:
person = {"name": "Liam", "age": 25}
person_copy = person.copy()
person_copy["age"] = 26
print("Original:", person)
print("Copy:", person_copy)
Output:
Original: {'name': 'Liam', 'age': 25}
Copy: {'name': 'Liam', 'age': 26}
What happened here:
person.copy()created a new dictionary- Changing
person_copy["age"]changed only the copied dictionary - The original dictionary stayed the same
This works as beginners usually expect when the dictionary contains simple values like strings, numbers, or booleans.
Shallow copy explained #
copy() makes a shallow copy.
That means:
- The top-level dictionary is new
- But nested mutable values are still shared
- This includes values like lists and dictionaries inside the dictionary
Example:
student = {
"name": "Ana",
"grades": [90, 85]
}
student_copy = student.copy()
student_copy["grades"].append(100)
print("Original:", student)
print("Copy:", student_copy)
print(student is student_copy)
print(student["grades"] is student_copy["grades"])
Output:
Original: {'name': 'Ana', 'grades': [90, 85, 100]}
Copy: {'name': 'Ana', 'grades': [90, 85, 100]}
False
True
Notice the difference:
student is student_copyisFalse, so they are different dictionariesstudent["grades"] is student_copy["grades"]isTrue, so both dictionaries share the same list
Because of that, changing the nested list in the copy also changed the original.
If you want to understand this in more detail, see Python shallow copy vs deep copy explained.
When to use copy() #
Use copy() when:
- You want to edit a dictionary without changing the original
- You need a backup before making updates
- You pass a dictionary to code that may modify it
- Your dictionary contains only simple values, so a shallow copy is enough
Example:
settings = {"theme": "dark", "font_size": 14}
settings_backup = settings.copy()
settings["font_size"] = 16
print("Current:", settings)
print("Backup:", settings_backup)
Output:
Current: {'theme': 'dark', 'font_size': 16}
Backup: {'theme': 'dark', 'font_size': 14}
This is a common pattern before using methods that change a dictionary, such as dict.update() or dict.clear().
copy() vs assignment #
This is one of the most common beginner mistakes.
Using = does not make a new dictionary. It only creates another variable that refers to the same object.
Using assignment #
original = {"x": 1, "y": 2}
assigned = original
assigned["x"] = 99
print("Original:", original)
print("Assigned:", assigned)
print(original is assigned)
Output:
Original: {'x': 99, 'y': 2}
Assigned: {'x': 99, 'y': 2}
True
assigned = original shares one dictionary.Using copy() #
original = {"x": 1, "y": 2}
copied = original.copy()
copied["x"] = 99
print("Original:", original)
print("Copied:", copied)
print(original is copied)
Output:
Original: {'x': 1, 'y': 2}
Copied: {'x': 99, 'y': 2}
False
original.copy() makes a separate dictionary.Summary:
assigned = original→ same dictionarycopied = original.copy()→ new dictionary
Return value and method behavior #
copy():
- Returns a dictionary
- Does not modify the original dictionary
- Copies the contents as they are at the time of copying
- Keeps later top-level changes separate between the two dictionaries
Example:
data = {"a": 1, "b": 2}
data_copy = data.copy()
data["a"] = 10
data_copy["b"] = 20
print("Original:", data)
print("Copy:", data_copy)
Output:
Original: {'a': 10, 'b': 2}
Copy: {'a': 1, 'b': 20}
Common mistakes #
Here are some common problems beginners run into with dict.copy():
- Using
=instead ofcopy()and expecting an independent dictionary - Assuming
copy()also duplicates nested lists and nested dictionaries - Forgetting to save the return value of
copy() - Changing a nested value in the copied dictionary and being surprised that the original also changes
These print statements can help you debug:
print(original)
print(copied)
print(original is copied)
print(id(original), id(copied))
print(id(original["key"]))
print(id(copied["key"]))
What these help you check:
- Whether the dictionaries have the same contents
- Whether the two variables point to the same dictionary
- Whether a nested value is shared between both dictionaries
If you need to copy nested data structures too, you may need a deep copy. A related example is how to copy a list in Python.
FAQ #
Does dict.copy() change the original dictionary? #
No. It returns a new dictionary and leaves the original unchanged.
Is dict.copy() a deep copy? #
No. It is a shallow copy. Nested mutable objects are still shared.
What is the difference between copy() and =? #
copy() creates a new dictionary object. = makes a new variable name pointing to the same dictionary.
When should I use a deep copy instead? #
Use a deep copy when your dictionary contains nested mutable objects and you want fully independent copies.