How to Copy a Dictionary in Python?

When I first started working with Python dictionaries more than a decade ago, I often ran into a simple but frustrating problem: copying dictionaries.

At first, I thought assigning one dictionary to another would give me a new copy. But I quickly learned that it only created a reference, not a true copy. This meant any changes in one affected the other.

I’ve used many different methods to copy dictionaries in real projects, sometimes while handling JSON API responses, other times while cleaning survey data for employees. In this tutorial, I’ll show you six practical ways to copy a dictionary in Python.

Method 1 – Use the copy() Method

The simplest way to copy a dictionary is by using Python’s built-in .copy() method.

# Original dictionary
employee = {"name": "John", "state": "Texas", "age": 32}

# Copy dictionary using copy()
employee_copy = employee.copy()

# Modify the copy
employee_copy["age"] = 40

print("Original:", employee)
print("Copy:", employee_copy)

You can refer to the screenshot below to see the output:

python copy dictionary

This method works perfectly for shallow copies, meaning it only copies the first level of keys and values. If your dictionary has nested dictionaries, changes in the nested part will still affect the original.

I often use this when working with flat employee or product dictionaries where I don’t need to worry about nested structures.

Method 2 – Use the dict() Constructor

Another quick way is to pass the dictionary into the dict() constructor in Python.

employee = {"name": "Alice", "state": "California", "skills": ["Excel", "Python"]}

# Copy dictionary using dict()
employee_copy = dict(employee)

employee_copy["state"] = "New York"

print("Original:", employee)
print("Copy:", employee_copy)

You can refer to the screenshot below to see the output:

copy dictionary python

This gives the same result as .copy(). It’s useful when I want to make it explicit in my code that I’m creating a new dictionary.

Like .copy(), this is a shallow copy, so nested objects are still linked.

Method 3 – Use Dictionary Comprehension

Dictionary comprehensions let me copy and filter keys in one go.

employee = {"name": "Mike", "state": "Florida", "salary": 70000}

# Copy dictionary using comprehension
employee_copy = {key: value for key, value in employee.items()}

print("Original:", employee)
print("Copy:", employee_copy)

You can refer to the screenshot below to see the output:

python dict copy

This method is flexible. For example, I can exclude a key while copying:

employee_copy = {k: v for k, v in employee.items() if k != "salary"}

I often use this when I need a copy but also want to remove sensitive data like salaries before exporting reports.

Method 4 – Use a For Loop

Sometimes, I prefer the old-school way, a simple loop.

employee = {"name": "Sarah", "state": "Ohio", "role": "Manager"}

# Copy dictionary using for loop
employee_copy = {}
for key, value in employee.items():
    employee_copy[key] = value

print("Original:", employee)
print("Copy:", employee_copy)

This approach is slower than comprehension but very readable for beginners. I’ve used it when teaching Python workshops in the U.S., as it’s easier for new developers to understand.

Method 5 – Use copy.deepcopy()

When dealing with nested dictionaries, shallow copies won’t cut it. That’s when I use deepcopy from the copy module.

import copy

employee = {
    "name": "David",
    "state": "Illinois",
    "projects": {"Q1": "Budget Analysis", "Q2": "Forecasting"}
}

# Deep copy
employee_copy = copy.deepcopy(employee)

employee_copy["projects"]["Q1"] = "Market Research"

print("Original:", employee)
print("Copy:", employee_copy)

Here, modifying the nested dictionary in the copy does not affect the original. This is extremely useful when working with hierarchical data, like organizational structures or nested JSON from APIs.

Method 6 – Use Assignment (= Operator)

Finally, let me show you what not to do if you want a real copy.

employee = {"name": "Emma", "state": "New York"}

# Assignment creates a reference, not a copy
employee_copy = employee

employee_copy["state"] = "Nevada"

print("Original:", employee)
print("Copy:", employee_copy)

Both dictionaries change because they point to the same memory location. I only use this when I intentionally want two variables to reference the same dictionary.

Copying dictionaries in Python may seem simple, but the method you choose depends on your data.

  • For flat dictionaries, .copy() or dict() works fine.
  • For filtering or modifying data, comprehension is my go-to.
  • For nested dictionaries, deepcopy() is the safest option.

Over the years, I’ve switched between these methods depending on the project. Once you understand the difference between shallow and deep copies, you’ll know exactly which method to use.

You may also read other dictionary articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.