Python dict.copy() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
dict method

Definition

copy() returns a new dict with the same key/value pairs in the same order. Adding, replacing, or deleting keys in the copy never affects the original, and the other way round.

It is a shallow copy: the values themselves are not duplicated. If a value is a list or dict, both dicts point to that same inner object, so changing it through one is visible through the other. For fully independent nested data, use copy.deepcopy() after import copy.

dict.copy() is an instance method of the built-in dict type: call it on a dictionary with dot notation, such as defaults.copy(...). No import is needed.

Syntax

Call it with no arguments and keep the returned dict.

python
new_dict = d.copy()
CallWhat it does
{"a": 1, "b": 2}.copy()Returns {'a': 1, 'b': 2}, a new dict object.
{"k": [1, 2]}.copy()Returns {'k': [1, 2]}; the inner list is shared, not copied.
copy.deepcopy(d)Copies nested lists and dicts as well (needs import copy).

dict(d) and {**d} make the same shallow copy as d.copy(). Plain assignment, new = d, makes no copy at all.

Method signatures
python
dict.copy()
# returns: a new dict with the same keys and values (shallow copy)

Parameters

copy() takes no parameters.

Return Value

Return ValueDescription
dictA new dict, equal to the original (== is True) but a different object (is is False). Its values are the same objects as the original's.

Python Examples

Example 1 — Customize a Copy of Editor Defaults

Start a user's preferences from the defaults, then change, increment, and delete keys in the copy only.

python
defaults = {"theme": "light", "font_size": 14, "autosave": True}
user = defaults.copy()
user["theme"] = "dark"
user["font_size"] += 2
del user["autosave"]

print(defaults)
print(user)
print(user is defaults, user == defaults)

How It Works

  1. Separate top level. defaults.copy() builds a second dict, so assignments and del on user leave defaults alone.
  2. Increment safely. user["font_size"] += 2 stores the new integer 16 in the copy only.
  3. Deleted in one. autosave disappears from the copy and stays in the defaults.
  4. Different and unequal. They were never the same object, and after the edits their contents differ too.

Example 2 — Watch a Shared Player List Change in Both Dicts

Copy a team record shallowly and deeply, then change the nested list through the shallow copy.

python
import copy

roster = {"team": "Falcons", "players": ["Ava", "Ben"]}
shallow = roster.copy()
deep = copy.deepcopy(roster)

shallow["players"].append("Cara")
shallow["team"] = "Hawks"

print(roster)
print(shallow)
print(deep)
print(shallow["players"] is roster["players"])

How It Works

  1. Shared inner list. The shallow copy's players value is the very list stored in roster, so appending Cara shows up in both.
  2. Replaced value stays local. Assigning "Hawks" rebinds the key in the copy only; the original team is still Falcons.
  3. Deep copy is independent. copy.deepcopy() duplicated the list too, so deep keeps just Ava and Ben.
  4. Identity proof. is confirms that the shallow copy and the original hold one list object.

Example 3 — Remove Sold-Out Stock Without RuntimeError

Deleting keys from the dict you are looping over fails. Looping over a copy fixes it.

python
stock = {"pens": 12, "erasers": 0, "rulers": 5, "glue": 0}

try:
    for item in stock:
        if stock[item] == 0:
            del stock[item]
except RuntimeError:
    print("RuntimeError: size changed during the loop")
print(stock)

for item, qty in stock.copy().items():
    if qty == 0:
        del stock[item]
print(stock)

How It Works

  1. Fails on the next step. Deleting erasers works, but when the loop asks for the next key Python notices the size changed and raises RuntimeError.
  2. Half done. The loop stopped early, so glue with 0 left is still in the dict.
  3. Loop over a snapshot. stock.copy().items() iterates a separate dict, so deleting from stock is safe.
  4. Clean result. Only the items with stock left remain; list(stock) would work as a snapshot of the keys too.

Frequently Asked Questions

No. It makes a shallow copy: a new dict whose values are the same objects. Nested lists and dicts are shared with the original.
new = old creates a second name for the same dict, so changes show in both. new = old.copy() creates a separate dict.
Use import copy and then copy.deepcopy(d). It recursively copies nested lists, dicts, and other containers.
For a plain dict, yes: both return a new shallow copy with the same keys, values, and order. {**d} does the same.
Adding or removing keys while looping over a dict raises RuntimeError. Looping over d.copy() or list(d) lets you change the original safely.

Next: dict.fromkeys()

Build a new dict from a list of keys, all set to the same value.

fromkeys() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful