dict.copy()

dict.copy()
Returns: dict · Updated March 13, 2026 · Dict Methods
dictionaries methods copying mutation

The .copy() method returns a shallow copy of a dictionary. This creates a new dictionary object containing references to the same values as the original—meaning the keys and values themselves are copied, but nested objects are not recursively copied. Both the original and the copy can be modified independently.

Syntax

dict.copy()

Parameters

None. This method takes no parameters.

Return Value

Returns a new dictionary that is a shallow copy of the original. The returned type is dict.

Examples

Basic usage - creating an independent copy

Create a separate copy that will not affect the original:

original = {"name": "Alice", "age": 30}
copy = original.copy()

copy["age"] = 31

print(original)
# {'name': 'Alice', 'age': 30}

print(copy)
# {'name': 'Alice', 'age': 31}

Copying a dictionary before modification

A common pattern is to copy a dictionary before making changes:

config = {"host": "localhost", "port": 8080, "timeout": 30}
config_copy = config.copy()

config_copy["timeout"] = 60
config_copy["new_option"] = True

print(config)
# {'host': 'localhost', 'port': 8080, 'timeout': 30}

print(config_copy)
# {'host': 'localhost', 'port': 8080, 'timeout': 60, 'new_option': True}

Shallow copy behavior with nested objects

Important: nested objects are shared between original and copy:

original = {"name": "Alice", "scores": [95, 87, 92]}
copy = original.copy()

# Modify the nested list in the copy
copy["scores"].append(100)

print(original["scores"])
# [95, 87, 92, 100]  # Original is also affected!

print(copy["scores"])
# [95, 87, 92, 100]

Deep copy for nested structures

For nested dictionaries or lists, use copy.deepcopy():

import copy

original = {"name": "Alice", "scores": [95, 87, 92]}
deep_copy = copy.deepcopy(original)

deep_copy["scores"].append(100)

print(original["scores"])
# [95, 87, 92]  # Original is unaffected!

print(deep_copy["scores"])
# [95, 87, 92, 100]

Copy vs dict() constructor

The .copy() method and the dict() constructor produce equivalent results:

source = {"a": 1, "b": 2}

# Both produce the same result
copy1 = source.copy()
copy2 = dict(source)

print(copy1 == copy2)
# True

However, .copy() is slightly faster since it avoids the overhead of the constructor.

Using copy with dictionary merging

Create a merged dictionary starting from a copy:

defaults = {"theme": "dark", "language": "en", "notifications": True}
user_overrides = {"theme": "light"}

# Start with a copy of defaults
result = defaults.copy()
result.update(user_overrides)

print(result)
# {'theme': 'light', 'language': 'en', 'notifications': True}

Common Patterns

Passing a dictionary without mutating the original

def process_config(config):
    # Work on a copy to avoid side effects
    config = config.copy()
    config["processed"] = True
    return config

original = {"host": "localhost", "port": 8080}
processed = process_config(original)

print(original)
# {'host': 'localhost', 'port': 8080}

print(processed)
# {'host': 'localhost', 'port': 8080, 'processed': True}

Caching with copies

cache = {}

def get_user(user_id):
    if user_id in cache:
        # Return a copy to prevent external modification
        return cache[user_id].copy()
    
    # Fetch from database (simulated)
    user = {"id": user_id, "name": f"User {user_id}"}
    cache[user_id] = user
    return user.copy()

Dictionary comprehension as alternative

Dictionary comprehension can also create copies with transformations:

original = {"a": 1, "b": 2, "c": 3}

# Simple copy via comprehension
copy = {k: v for k, v in original.items()}

# Copy with modifications
doubled = {k: v * 2 for k, v in original.items()}

print(copy)
# {'a': 1, 'b': 2, 'c': 3}

print(doubled)
# {'a': 2, 'b': 4, 'c': 6}

Shallow vs Deep Copy

Aspect.copy() (shallow)copy.deepcopy()
PerformanceFastSlower
Nested objectsNot copied (shared)Recursively copied
Use caseSimple dictionariesNested dicts/lists

For simple dictionaries with immutable values (strings, numbers, tuples), .copy() is perfect since there are no nested objects to worry about.

See Also

  • dict() — the dict constructor for creating new dictionaries
  • dict.items() — returns key-value pairs, useful when copying
  • copy module — provides deepcopy() for nested structures