How to Copy a dictionary in Python

In this tutorial, we are going to understand how to copy a dictionary in Python. We can copy all the contents of one dictionary to another dictionary with the help of copy() method or assignment operator. The difference between copying using assignment operator and using copy() is that when we copy a dictionary using assignment operator, any operation done on the new dictionary which contains the copied elements will also make changes in the original dictionary.

On the other hand, if we copy the contents of a dictionary using copy() method any changes made to the dictionary containing copied elements will not affect the original dictionary.

Python program to copy dictionary

Contents of the dictionary:

marks={‘Operating System’:74,’Java’:72,’Python’:88,’c++’:82}

 

Copying the contents of the dictionary using an assignment operator

marks={'Operating System':74,'Java':72,'Python':88,'c++':82}
copy_marks=marks
copy_marks

Copying the content of marks to copy_marks.

 

Output

Output
{}
{}

Leave a Reply

Your email address will not be published. Required fields are marked *