Python Dictionary – Make a copy of a dictionary

In this post we will be creating a Python script that will make a copy of an existing dictionary, dictionaries provide a means of storing multiple pieces of data in to a single variable. This code could apply where you are wanting to make changes to an existing dictionary but are still wanting to retain a copy of the original.

See below the dictionary we will be using for this example.

Dictionary={'Jan': 12, 'Feb': 16, 'Mar': 19, 'Apr': 22, 'May': 29}

The snippet of code below will create a copy of the above in to a new dictionary named ‘DictionaryCopy’.

Dictionary={'Jan': 12, 'Feb': 16, 'Mar': 19, 'Apr': 22, 'May': 29}

DictionaryCopy = Dictionary.copy()
print(DictionaryCopy)

The above would return the following as output.

{'Jan': 12, 'Feb': 16, 'Mar': 19, 'Apr': 22, 'May': 29}
>>> 

Leave a Reply