Python | Ways to Copy Dictionary

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output

initial dictionary =  {'name2': 'vashu', 'name': 'akshat', 'name1': 'manjeet'}
updated dictionary =  {'name2': 'vashu', 'name': 'akshat', 'name1': 'nikhil'}

Method#3 : Using Dictionary comprehension

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output

initial dictionary =  {'name': 'akshat', 'name2': 'vashu', 'name1': 'manjeet'}
updated dictionary =  {'name': 'akshat', 'name2': 'vashu', 'name1': 'ayush'}


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.