Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Copy Dictionaries (Complete Guide for Beginners)

 In Python, dictionaries are mutable objects, which means their values can be changed after creation. Because of this, copying a dictionary is an important concept when you want to work with data without affecting the original dictionary.

In this tutorial, you will learn different ways to copy dictionaries in Python with clear examples and explanations.


What is Copying a Dictionary?

Copying a dictionary means creating a new dictionary with the same data as the original one.

There are two types of copying:

  • Shallow Copy
  • Deep Copy (for nested dictionaries)

Example Dictionary

student = {
    "name": "John",
    "age": 20,
    "grade": "A"
}

Method 1: Using copy() Method

The copy() method creates a shallow copy of a dictionary.

Example

student = {
    "name": "John",
    "age": 20,
    "grade": "A"
}

new_student = student.copy()

print(new_student)

Output

{'name': 'John', 'age': 20, 'grade': 'A'}

Explanation

  • new_student is a new dictionary.
  • Changes in new_student do not affect student.

Method 2: Using dict() Constructor

You can also use the built-in dict() function.

Example

student = {
    "name": "John",
    "age": 20,
    "grade": "A"
}

new_student = dict(student)

print(new_student)

Output

{'name': 'John', 'age': 20, 'grade': 'A'}

Explanation

  • dict() creates a new dictionary from an existing one.

Method 3: Using Assignment (Not a Copy!)

This method does NOT create a new dictionary.

Example

student = {
    "name": "John",
    "age": 20
}

new_student = student

new_student["grade"] = "A"

print(student)

Output

{'name': 'John', 'age': 20, 'grade': 'A'}

Explanation

  • Both variables refer to the same dictionary.
  • Changes affect both.

Method 4: Copying Using Loop (Manual Copy)

Example

student = {
    "name": "John",
    "age": 20
}

new_student = {}

for key, value in student.items():
    new_student[key] = value

print(new_student)

Output

{'name': 'John', 'age': 20}

Explanation

  • Each key-value pair is copied manually.

Shallow Copy vs Deep Copy

Shallow Copy

  • Copies only the outer dictionary
  • Inner objects are shared

Deep Copy

  • Copies everything including nested objects
  • Requires copy module

Example: Shallow Copy Problem

original = {
    "name": "John",
    "scores": [80, 90]
}

new_dict = original.copy()

new_dict["scores"].append(100)

print(original)

Output

{'name': 'John', 'scores': [80, 90, 100]}

Explanation

  • Nested list is shared between both dictionaries.

Deep Copy Solution

Example

import copy

original = {
    "name": "John",
    "scores": [80, 90]
}

new_dict = copy.deepcopy(original)

new_dict["scores"].append(100)

print(original)

Output

{'name': 'John', 'scores': [80, 90]}

Explanation

  • Deep copy creates a completely independent dictionary.

Real-World Example: User Profile Copy

Example

user = {
    "username": "admin",
    "role": "editor"
}

backup_user = user.copy()

backup_user["role"] = "viewer"

print(user)
print(backup_user)

Output

{'username': 'admin', 'role': 'editor'}
{'username': 'admin', 'role': 'viewer'}

Real-World Example: Product Inventory Backup

Example

inventory = {
    "Laptop": 10,
    "Mouse": 25
}

backup_inventory = dict(inventory)

backup_inventory["Laptop"] = 5

print(inventory)
print(backup_inventory)

Common Mistakes

Mistake 1: Using Assignment Instead of Copy

❌ Wrong

new_dict = original

✔ This does NOT create a copy.


Mistake 2: Not Using deepcopy for Nested Data

❌ Problem

copy_dict = original.copy()

✔ Works only for simple dictionaries.


Correct Solution

import copy
copy.deepcopy(original)

Dictionary Copy Methods Summary

MethodTypeDescription
copy()ShallowCopies dictionary
dict()ShallowCreates new dictionary
=Not copySame reference
Loop copyShallowManual copying
deepcopy()DeepFull independent copy

Practice Exercise 1

Create a copy of this dictionary:

person = {
    "name": "Tom",
    "age": 30
}

Practice Exercise 2

Modify the copied dictionary without changing the original.

data = {
    "city": "London",
    "population": 9000000
}

Conclusion

Copying dictionaries in Python is essential when working with data that should not be modified accidentally.

You learned:

  • copy() method
  • dict() constructor
  • Assignment vs real copy
  • Deep copy using copy.deepcopy()
  • Real-world use cases

Understanding dictionary copying helps you avoid bugs in data handling, APIs, databases, and applications.




Post a Comment

0 Comments