|
| Home |
|---|
The copy() method creates a new dictionary object containing the same key-value pairs as the original. It's a built-in shortcut for duplicating a dictionary without writing a loop, and the most common way to make sure you're working with a genuinely separate object rather than a second name for the same one.
This page covers the exact syntax, parameters, and return value of
copy(), how to confirm it really creates a new object, and
where its shallow-copy behavior can catch you out. For a full comparison of
copy() against dict() and deepcopy(),
see the broader Copy a Dictionary in
Python guide.
new_dictionary = dictionary.copy()
| Parameters | None — copy() accepts no arguments |
|---|---|
| Return value | A new dictionary object containing the same key-value pairs as the original |
| Copy type | Shallow copy — see the "Shallow Copy Behavior" section below |
cart = {
"item": "Keyboard",
"price": 45,
"qty": 1
}
cart_copy = cart.copy()
print(cart_copy)
{'item': 'Keyboard', 'price': 45, 'qty': 1}
cart_copy contains the same data as cart, but it's
a distinct dictionary object stored in its own variable.
It's easy to see that a copy behaves independently, but you can
also confirm it directly using Python's built-in id() function,
which returns a unique identifier for every object in memory.
cart = {"item": "Keyboard", "price": 45}
cart_copy = cart.copy()
print(id(cart))
print(id(cart_copy))
print(cart is cart_copy)
140312831324288
140312831325504
False
The two dictionaries have different id() values, and
cart is cart_copy returns False — confirming
they're genuinely separate objects in memory, even though
cart == cart_copy would return True since their
contents are equal.
cart = {
"item": "Keyboard",
"price": 45
}
cart_copy = cart.copy()
cart_copy["price"] = 39
print("Original:", cart)
print("Copy:", cart_copy)
Original: {'item': 'Keyboard', 'price': 45}
Copy: {'item': 'Keyboard', 'price': 39}
Changing price in cart_copy has no effect on
cart — they're independent at the top level.
cart = {
"item": "Keyboard",
"price": 45
}
cart_reference = cart # NOT a copy — same object, different name
cart_reference["price"] = 39
print(cart)
{'item': 'Keyboard', 'price': 39}
Since cart_reference = cart just gives the same dictionary a
second name, changing cart_reference changes cart
too. This is the single most common reason beginners reach for
copy() in the first place.
cart_reference = cart |
cart_copy = cart.copy() |
|
|---|---|---|
is comparison |
True (same object) |
False (different object) |
| Modifying it affects the original? | Yes | No (at the top level — see below) |
copy() only duplicates the dictionary's top level. If a value
is itself a list or dictionary, both the original and the copy still point
to that same nested object underneath.
game_state = {
"player": "Alice",
"inventory": ["sword", "shield"]
}
game_state_copy = game_state.copy()
game_state_copy["inventory"].append("potion")
print("Original:", game_state)
print("Copy:", game_state_copy)
Original: {'player': 'Alice', 'inventory': ['sword', 'shield', 'potion']}
Copy: {'player': 'Alice', 'inventory': ['sword', 'shield', 'potion']}
Even though we only modified game_state_copy, the original's
inventory list changed too — copy() copied the
dictionary, but not the list living inside it. If you need nested data
fully independent as well, use copy.deepcopy() instead — see
the shallow vs. deep
copy comparison for the full explanation and more examples.
A common real-world use of copy() is capturing a dictionary's
current state before making changes — useful for undo functionality,
comparing before/after values, or rolling back on error.
employee = {
"name": "Alice",
"department": "Engineering",
"salary": 65000
}
previous_state = employee.copy()
employee["salary"] = 70000
employee["department"] = "Product"
print("Before:", previous_state)
print("After:", employee)
if employee["salary"] != previous_state["salary"]:
print(f"Salary changed from {previous_state['salary']} to {employee['salary']}")
Before: {'name': 'Alice', 'department': 'Engineering', 'salary': 65000}
After: {'name': 'Alice', 'department': 'Product', 'salary': 70000}
Salary changed from 65000 to 70000
Because previous_state is a genuine copy, it keeps the
original values even after employee is modified — letting you
compare the two states directly.
If you're working with a dictionary subclass — such as
defaultdict — calling copy()
returns a regular dict, not another instance of the subclass.
This is a subtle but important distinction if your code relies on the
subclass's special behavior.
from collections import defaultdict
counts = defaultdict(int)
counts["apples"] = 5
counts_copy = counts.copy()
print(type(counts))
print(type(counts_copy))
<class 'collections.defaultdict'>
<class 'collections.defaultdict'>
In this specific case, defaultdict.copy() does preserve the
subclass and its default factory — but this isn't guaranteed for every
dictionary subclass you might encounter, so it's worth verifying with
type() if your code depends on the copy behaving exactly like
the original.
=) instead of copy(), and being surprised when both variables change togethercopy() fully duplicates nested lists or dictionaries inside the original — it doesn'tis when == is what's actually needed, or vice versacopy() and expecting a return value other than a new dictionary — it never modifies the original in placeIt returns a new dictionary object containing the same key-value pairs as the original. The original dictionary is left unchanged.
copy() is a shallow copy. The top-level dictionary is
independent, but any nested lists or dictionaries inside it are still
shared with the original. Use copy.deepcopy() for a
fully independent copy.
Compare the two dictionaries with is, or check
id() on each. original is copy returns
False when copy() was used correctly, since
they're separate objects even though their contents are equal.
No — copy() takes no parameters. It always duplicates
the entire dictionary at the top level; there's no way to copy only
part of it with this method.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net