Python | Ways to Copy Dictionary
Last Updated :
30 Dec, 2022
Dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. It is widely used in day to day programming, web development, and machine learning. When we simply assign dict1 = dict2 it refers to the same dictionary. Let's discuss a few ways to copy the dictionary from another dictionary.
Method#1: Using copy() copy() method returns a shallow copy of the dictionary. It doesn't take any parameter and return a new dictionary which is not referring to the initial dictionary.
Python3
# Python3 code to demonstrate
# how to copy dictionary
# using copy() function
# initialising dictionary
test1 = {"name" : "akshat", "name1" : "manjeet", "name2" : "vashu"}
# method to copy dictionary using copy() function
test2 = test1.copy()
# updating test2
test2["name1"] ="nikhil"
# print initial dictionary
print("initial dictionary = ", test1)
# printing updated dictionary
print("updated dictionary = ", test2)
Output
initial dictionary = {'name1': 'manjeet', 'name2': 'vashu', 'name': 'akshat'}
updated dictionary = {'name1': 'nikhil', 'name': 'akshat', 'name2': 'vashu'}
Method #2: Using dict() The dict() is a constructor which creates dictionary in Python.
Python3
# Python3 code to demonstrate
# how to copy dictionary
# using dict()
# initialising dictionary
test1 = {"name" : "akshat", "name1" : "manjeet", "name2" : "vashu"}
# method to copy dictionary using dict
test2 = dict(test1)
# updating test2
test2["name1"] ="nikhil"
# print initial dictionary
print("initial dictionary = ", test1)
# printing updated dictionary
print("updated dictionary = ", test2)
Output
initial dictionary = {'name2': 'vashu', 'name': 'akshat', 'name1': 'manjeet'}
updated dictionary = {'name2': 'vashu', 'name': 'akshat', 'name1': 'nikhil'}
Method#3 : Using Dictionary comprehension
Python3
# Python3 code to demonstrate
# how to copy dictionary
# using dictionary comprehension
# initialising dictionary
test1 = {"name" : "akshat", "name1" : "manjeet", "name2" : "vashu"}
# method to copy dictionary using dictionary comprehension
test2 = {k:v for k, v in test1.items()}
# updating test2
test2["name1"] ="ayush"
# print initial dictionary
print("initial dictionary = ", test1)
# printing updated dictionary
print("updated dictionary = ", test2)
Output
initial dictionary = {'name': 'akshat', 'name2': 'vashu', 'name1': 'manjeet'}
updated dictionary = {'name': 'akshat', 'name2': 'vashu', 'name1': 'ayush'}
Method #4: Using copy module
One additional approach to copying a dictionary is to use the built-in function deepcopy from the copy module. This function creates a deep copy of the dictionary, meaning that it creates a new dictionary object with new memory addresses for both the keys and the values in the original dictionary. This is useful if you want to create a completely independent copy of the dictionary and any nested objects it contains, such as lists or other dictionaries.
Here's an example of how to use deepcopy to copy a dictionary:
Python3
from copy import deepcopy
# initializing dictionary
test1 = {"name": "akshat", "name1": "manjeet", "name2": "vashu"}
# method to copy dictionary using deepcopy
test2 = deepcopy(test1)
# updating test2
test2["name1"] = "nikhil"
# print initial dictionary
print("initial dictionary = ", test1)
# printing updated dictionary
print("updated dictionary = ", test2)
Outputinitial dictionary = {'name': 'akshat', 'name1': 'manjeet', 'name2': 'vashu'}
updated dictionary = {'name': 'akshat', 'name1': 'nikhil', 'name2': 'vashu'}
The time complexity of using deepcopy to copy a dictionary is O(n), where n is the number of keys and values in the dictionary. The space complexity is also O(n), as a new dictionary object with n keys and values is created in memory.
Similar Reads
Python Dictionary copy() Python Dictionary copy() method returns a shallow copy of the dictionary. let's see the Python Dictionary copy() method with examples: Â Examples Input: original = {1:'geeks', 2:'for'} new = original.copy() // Operation Output: original: {1: 'one', 2: 'two'} new: {1: 'one', 2: 'two'}Syntax of copy()
3 min read
Ways to create a dictionary of Lists - Python A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list
3 min read
Python Dictionary Comprehension Like List Comprehension, Python allows dictionary comprehensions. We can create dictionaries using simple expressions. A dictionary comprehension takes the form {key: value for (key, value) in iterable}Python Dictionary Comprehension ExampleHere we have two lists named keys and value and we are iter
4 min read
How To Convert Python Dictionary To JSON? In Python, a dictionary stores information using key-value pairs. But if we want to save this data to a file, share it with others, or send it over the internet then we need to convert it into a format that computers can easily understand. JSON (JavaScript Object Notation) is a simple format used fo
6 min read
Write a dictionary to a file in Python A dictionary store data using key-value pairs. Our task is that we need to save a dictionary to a file so that we can use it later, even after the program is closed. However, a dictionary cannot be directly written to a file. It must first be changed into a format that a file can store and read late
3 min read