In this article we will discuss how to create a shallow and deep copy of dictionary in Python.

Create Shallow copy of Dictionary using dict.copy()

Python’s dictionary provides a member function copy() i.e.

dict.copy()

It returns a shallow copy of the existing dictionary.

What is a shallow copy ?

Shallow copy means a new dictionary object will be created and reference to the objects in existing dictionary will be inserted in this.

Let’s understand by some examples,

Suppose we have a dictionary of with key string and value as int or list of int i.e.

# Dictionary of strings and ints
wordsDict = {
    "Hello": 56,
    "at" : 23 ,
    "test" : 43,
    "this" : 43,
    "who" : [56, 34, 44]
    }

Now create a Copy of this dictionary using dict.copy() i.e.
# create a Shallow copy  the original dictionary
newDict = wordsDict.copy()

Now this new dictionary will contain shallow copy of all the elements from original dictionary.

Contents of New Dictionary is :

"Hello": 56,
"at" : 23 ,
"test" : 43,
"this" : 43,
"who" : [56, 34, 44]

Let’s modify the new dictionary by changing the value of a key ‘at’ i.e.
# Modify the value of key in new dictionary
newDict["at"] = 200

Now Contents of new Dictionary is,
Modified New Dictionary :
test  ::  43
who  ::  [56, 34, 44]
at  ::  200
Hello  ::  56
this  ::  43

Now check the contents of original dictionary i.e.
Original Dictionary : 
test  ::  43
who  ::  [56, 34, 44]
at  ::  23
Hello  ::  56
this  ::  43

Value of key ‘at’ is different in both. So, modifying the new dictionary didn’t changed the original dictionary contents. Then how it is shallow ?

The word Shallow copy comes in picture when there is some object in dictionary like list or user define objects instead of primitive datatypes.

Now let’s modify the value of key that contains list as value i.e.

newDict["who"].append(222)

Now Contents of new Dictionary is,
Modified New dictionary : 
at  ::  200
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222]

Now check the contents of original dictionary i.e.
Original Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222]

We changed the contents of shallow copied dictionary but changes are now reflected in original dictionary too. It happened because reference to list object in original dictionary was copied in new dictionary. So, any changes in that list will be reflected in both the dictionaries.

To avoid this kind of thing we can use deepcopy() to create a deep copy of the dictionary.

Create a deep copy of dictionary using copy.deepcopy()

In Python copy module provides a function deepcopy() i.e.

import copy
'''
'''
dict = copy.deepcopy(dict)

It accepts a dictionary as parameter and returns a new dictionary with copy all the objects recursively.

Suppose we have a dictionary i.e.

# Dictionary of strings and ints
wordsDict = {
    "Hello": 56,
    "at" : 23 ,
    "test" : 43,
    "this" : 43,
    "who" : [56, 34, 44]
    }

Let’s create a deep copy of this dictionary,
# Create a deep copy of the dictionary
otherDict = copy.deepcopy(wordsDict)

Now any modification in this copy will not be reflected in original copy i.e.
'''
Modify the contents of list object in deep copied dictionary will 
have no impact on original dictionary because its a deep copy.
'''
newDict["who"].append(100)

Contents of Deep Copy Dictionary:
Modified Deep copy of Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222]

Contents of Original Dictionary:
Original Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222, 100]

Contents of both the dictionaries are different and modification in one will not affect other copy because its a deep copy.

Complete Series of Python Dictionary tutorials,

Subscribe with us to join a list of 2000+ programmers and get latest tips & tutorials at your inbox through our weekly newsletter.

Complete example is as follows :

import copy


def displayList(text, dictOfElements) :
    print("--------")
    print(text)
    for key , value in dictOfElements.items():
        print(key, " :: ", value)

def main():
    
    # Dictionary of strings and ints
    wordsDict = {
        "Hello": 56,
        "at" : 23 ,
        "test" : 43,
        "this" : 43,
        "who" : [56, 34, 44]
        }
    
    '''
    Shallow Copying dictionaries using dict.copy()
    '''
    print("***** Shallow Copy *********")
    
    displayList("Original Dictionary : " , wordsDict)
    
    # create a Shallow copy  the original dictionary
    newDict = wordsDict.copy()
    
    # Modify the value of key in new dictionary
    newDict["at"] = 200
    
    print("Contents of copied dictionary changed")

    displayList("Modified copied dictionary : " , newDict)
    
    displayList("Original Dictionary : " , wordsDict)

    '''
    Modify the contents of list object in shallow copied dictionary will 
    modify the contents of original dictionary too because its a shallow copy. 
    '''
    newDict["who"].append(222)
    
    print("Contents of list in copied dictionary changed")

    displayList("Modified copied dictionary : " , newDict)
    
    displayList("Original Dictionary : " , wordsDict)

    print("***** Deep Copy *******")
    
    displayList("Original Dictionary : " , wordsDict)
    
    # Create a deep copy of the dictionary
    otherDict = copy.deepcopy(wordsDict)
    
    displayList("Deep copy of  Dictionary : " , otherDict)
    
    '''
    Modify the contents of list object in deep copied dictionary will 
    have no impact on original dictionary because its a deep copy.
    '''
    newDict["who"].append(100)

    displayList("Modified Deep copy of Dictionary : " , otherDict)
    displayList("Original Dictionary : " , wordsDict)
    
if __name__ == '__main__':
    main()

Output:
***** Shallow Copy *********
--------
Original Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44]
Contents of copied dictionary changed
--------
Modified copied dictionary : 
at  ::  200
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44]
--------
Original Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44]
Contents of list in copied dictionary changed
--------
Modified copied dictionary : 
at  ::  200
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222]
--------
Original Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222]
***** Deep Copy *******
--------
Original Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222]
--------
Deep copy of  Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222]
--------
Modified Deep copy of Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222]
--------
Original Dictionary : 
at  ::  23
this  ::  43
Hello  ::  56
test  ::  43
who  ::  [56, 34, 44, 222, 100]