Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Copy Dictionaries

Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Copy Dictionaries

Copy Dictionaries

When working with dictionaries in Python, sometimes you need to make a duplicate so you can modify it without changing the original. Python gives you two easy ways to do this.

Method 1: Using copy()

The copy() method creates a separate copy of the dictionary. Changes made to the copied version will not affect the original.

				
					student = {
    'name': 'Ayesha',
    'grade': 'A',
    'passed': True,
    'year': 2024
}

backup = student.copy()

print("Original:", student)
print("Copied:", backup)

				
			

Output:

				
					Original: {'name': 'Ayesha', 'grade': 'A', 'passed': True, 'year': 2024}
Copied: {'name': 'Ayesha', 'grade': 'A', 'passed': True, 'year': 2024}

				
			

Method 2: Using dict() Function

Another way to copy a dictionary is by using the built-in dict() function. It creates a new dictionary using the key-value pairs from the original one.

				
					student = {
    'name': 'Ayesha',
    'grade': 'A',
    'passed': True,
    'year': 2024
}

backup = dict(student)

print("Original:", student)
print("Copied using dict():", backup)

				
			

Output:

				
					Original: {'name': 'Ayesha', 'grade': 'A', 'passed': True, 'year': 2024}
Copied using dict(): {'name': 'Ayesha', 'grade': 'A', 'passed': True, 'year': 2024}

				
			

Why Make a Copy?

  • To try out changes without touching the original data

  • To create backups or temporary versions

  • To use the same base dictionary in multiple places safely

Both copy() and dict() work well for this task. Use whichever you find more readable in your code.

Scroll to Top