Sayantan's Blog On Python Programming

PYTHON DICTIONARY SHALLOW COPY OPERATION

PYTHON DICTIONARY SHALLOW COPY OPERATION

INTRODUCTION

There are two types of copy operations are available in Python.

  • SHALLOW COPY
  • DEEP COPY

In this article we will discuss on SHALLOW copy operations. Shallow copy operation represents that if we change or modify any of the dictionary then the other dictionary will also be impacted.

There are several ways we can create a shallow copy of a dictionary in python.

  • Assignment Operation
  • Using copy() function for complex data type
  • Import copy module for complex data type

Assignment Operation

# PYTHON DICTIONARY SHALLOW COPY OPERATION
# Example 1

dict_class_wise_prog_orig = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog_orig ->", dict_class_wise_prog_orig)

dict_class_wise_prog_copy = dict_class_wise_prog_orig
print("\nList of key value pair of dict_class_wise_prog_copy ->", dict_class_wise_prog_copy)

dict_class_wise_prog_orig['D'] = 'PHP'
dict_class_wise_prog_orig['E'] = 'XML'

print("\nList of key value pair of dict_class_wise_prog_orig ->", dict_class_wise_prog_orig)
print("\nList of key value pair of dict_class_wise_prog_copy ->", dict_class_wise_prog_copy)

PYTHON DICTIONARY SHALLOW COPY OPERATION : Output

List of key value pair of dict_class_wise_prog_orig -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog_copy -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog_orig -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

List of key value pair of dict_class_wise_prog_copy -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

In the above example, we have created a copy of dictionary dict_class_wise_prog_orig by using assignment operator. We have added two new key value pair and the impact is visible in copied dictionary. So this is an example of shallow copy operation.

# PYTHON DICTIONARY SHALLOW COPY OPERATION
# Example 2

dict_class_wise_prog_orig = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog_orig ->", dict_class_wise_prog_orig)

dict_class_wise_prog_copy = dict_class_wise_prog_orig
print("\nList of key value pair of dict_class_wise_prog_copy ->", dict_class_wise_prog_copy)

dict_class_wise_prog_orig['D'] = 'PHP'
dict_class_wise_prog_copy['E'] = 'XML'

print("\nList of key value pair of dict_class_wise_prog_orig ->", dict_class_wise_prog_orig)
print("\nList of key value pair of dict_class_wise_prog_copy ->", dict_class_wise_prog_copy)

PYTHON DICTIONARY SHALLOW COPY OPERATION : Output

List of key value pair of dict_class_wise_prog_orig -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog_copy -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog_orig -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

List of key value pair of dict_class_wise_prog_copy -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

In the above example, we have updated both the original dictionary and the copied dictionary. Both have the impact of one on another. So this is an example of shallow copy operation.

# PYTHON DICTIONARY SHALLOW COPY OPERATION
# Example 3


dict_class_wise_count_orig = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)

dict_class_wise_count_copy = dict_class_wise_count_orig
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)

dict_class_wise_count_orig.pop('E')
dict_class_wise_count_copy['F'] = 600

print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)

PYTHON DICTIONARY SHALLOW COPY OPERATION : Output

List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'F': 600}

List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'F': 600}

In the above example, we have removed the key “E” from dictionary dict_class_wise_count_orig and added one key form the copied dictionary. Both have the impact of one on another. This is an example of shallow copy operation.

Using copy() function for complex data type

We an use the built-in copy() method to create the copy of a dictionary.

# PYTHON DICTIONARY SHALLOW COPY OPERATION
# Example 4

dict_class_wise_prog_orig = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog_orig ->", dict_class_wise_prog_orig)

dict_class_wise_prog_copy = dict_class_wise_prog_orig.copy()
print("\nList of key value pair of dict_class_wise_prog_copy ->", dict_class_wise_prog_copy)

dict_class_wise_prog_orig['D'] = 'PHP'
dict_class_wise_prog_copy['E'] = 'XML'

print("\nList of key value pair of dict_class_wise_prog_orig ->", dict_class_wise_prog_orig)
print("\nList of key value pair of dict_class_wise_prog_copy ->", dict_class_wise_prog_copy)

PYTHON DICTIONARY SHALLOW COPY OPERATION : Output

List of key value pair of dict_class_wise_prog_orig -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog_copy -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog_orig -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP'}

List of key value pair of dict_class_wise_prog_copy -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'E': 'XML'}

In the above example, we have used the built-in copy() function to create the copy of dictionary dict_class_wise_prog_orig. Then we have modified both the original and copied dictionaries separately. Both do not have the impact of one on another. This is not an example of shallow copy operation. But, if we use complex data type like list as dictionary values then shallow copy will be allowed by python.

# PYTHON DICTIONARY SHALLOW COPY OPERATION
# Example 5

int_list = [500, 501, 502]
dict_class_wise_count_orig = {'A':100, 'B':200, 'C':300, 'D':400, 'E':int_list}
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)

dict_class_wise_count_copy = dict_class_wise_count_orig.copy()
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)

int_list.append(503)

print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)

PYTHON DICTIONARY SHALLOW COPY OPERATION : Output

List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}

List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}

List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502, 503]}

List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502, 503]}

In the above example, we have used a list item against the key “E“. We have changed the list item by adding one new element 503 it it. Now we can see that the original and copied dictionaries are in sync. So, copy() method will allow shallow copy only in case of complex data type like list. For simple data type like integer, string it will now allow shallow copy.

Import copy module for complex data type

Like copy() function, copy module is also allow shallow copy only for complex data type.

# PYTHON DICTIONARY SHALLOW COPY OPERATION
# Example 6

import copy


int_list = [500, 501, 502]
dict_class_wise_count_orig = {'A':100, 'B':200, 'C':300, 'D':400, 'E':int_list}
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)

dict_class_wise_count_copy = copy.copy(dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)

int_list.append(503)

print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)

PYTHON DICTIONARY SHALLOW COPY OPERATION : Output

List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}

List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}

List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502, 503]}

List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502, 503]}

In the above example, copy() of copy module has allowed the shallow copy operation.

PYTHON DICTIONARY SHALLOW COPY OPERATION : Output
PYTHON DICTIONARY SHALLOW COPY OPERATION : Output

RELATED TOPICS:

1 thought on “PYTHON DICTIONARY SHALLOW COPY OPERATION”

  1. Pingback: PYTHON DICTIONARY SORTING OPERATIONS - Sayantan's Blog On Python Programming

Leave a Comment

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