PYTHON LIST SHALLOW COPY
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 list then the other list will also be impacted.
There are several ways we can create a shallow copy of a list in python.
- Assignment Operation
- Importing Copy Module
Assignment Operation
# Copy operation using assignment operator example 1
org_list = [400, 200, 600, 100, 150, 500]
print("\nElement of original list ->", org_list)
copy_list = org_list
print("\nElement of copied list ->", copy_list)
PYTHON LIST SHALLOW COPY : Output
Element of original list -> [400, 200, 600, 100, 150, 500]
Element of copied list -> [400, 200, 600, 100, 150, 500]
In the above example we can see that with the assignment operator we have created a copied list and both the original and copied list have identical elements.
Now we will check the impact of changes in one list on the other list.
# Copy operation using assignment operator example 2
org_list = [400, 200, 600, 100, 150, 500]
print("\nElement of original list ->", org_list)
copy_list = org_list
print("\nElement of copied list ->", copy_list)
# Removing the last element from the copied list
copy_list.pop()
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
# UPdating the first element of the original list
org_list[0] = 700
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
PYTHON LIST SHALLOW COPY : Output
Element of original list -> [400, 200, 600, 100, 150, 500]
Element of copied list -> [400, 200, 600, 100, 150, 500]
Element of original list -> [400, 200, 600, 100, 150]
Element of copied list -> [400, 200, 600, 100, 150]
Element of original list -> [700, 200, 600, 100, 150]
Element of copied list -> [700, 200, 600, 100, 150]
In the above example we have removed the last element of copied list with the pop() method. That has automatically removed the same elements from the original list. Then we have updated the first element of original list and that also automatically updated the same element of copied list.
Importing Copy Module
Python provides several built-in modules which can be utilized in our programs to make it more efficient and productive. To use the built-in modules we will use the import command in our program. By reusing the codes of built-in modules our programs will look compact and easy to understand.
# Copy operation using copy() example 1
import copy
org_list = [400, 200, 600, 100, 150, 500]
print("\nElement of original list ->", org_list)
copy_list = copy.copy(org_list)
print("\nElement of copied list ->", copy_list)
PYTHON LIST SHALLOW COPY : Output
Element of original list -> [400, 200, 600, 100, 150, 500]
Element of copied list -> [400, 200, 600, 100, 150, 500]
In the above example we can see that with the copy() method of copy module we have created a copied list and both the original and copied list have identical elements.
Now we will check the impact of changes in one list on the other list.
# Copy operation using copy() example 2
import copy
org_list = [400, 200, 600, 100, 150, 500]
print("\nElement of original list ->", org_list)
copy_list = copy.copy(org_list)
print("\nElement of copied list ->", copy_list)
# Removing the last element from the copied list
copy_list.remove(600)
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
# UPdating the first element of the original list
org_list[0] = 700
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
PYTHON LIST SHALLOW COPY : Output
Element of original list -> [400, 200, 600, 100, 150, 500]
Element of copied list -> [400, 200, 600, 100, 150, 500]
Element of original list -> [400, 200, 600, 100, 150, 500]
Element of copied list -> [400, 200, 100, 150, 500]
Element of original list -> [700, 200, 600, 100, 150, 500]
Element of copied list -> [400, 200, 100, 150, 500]
# Copy operation using copy() example 3
import copy
org_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']
print("\nElement of original list ->", org_list)
copy_list = copy.copy(org_list)
print("\nElement of copied list ->", copy_list)
# Removing the last element from the copied list
copy_list.remove('PHP')
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
# UPdating the first element of the original list
org_list[1] = 'Word'
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
PYTHON LIST SHALLOW COPY : Output
Element of original list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']
Element of copied list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']
Element of original list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']
Element of copied list -> ['Python', 'Java', 'C++', 'C', 'XML']
Element of original list -> ['Python', 'Word', 'C++', 'C', 'PHP', 'XML']
Element of copied list -> ['Python', 'Java', 'C++', 'C', 'XML']
In the above example we can see that the change in both the original and copied list do not have the impact of one on another. Basically, for primitive data types like integer and strings, copy will behave like deep copy.
But, if the list contains any complex data type, then copy() will behave like shallow copy.
# Copy operation using copy() example 4
import copy
int_list = [1, 2, 5]
str_list = ['Python', 'Java', 'XML']
org_list = [400, 200, 600, 100, 150, 500, int_list, str_list]
print("\nElement of original list ->", org_list)
copy_list = copy.copy(org_list)
print("\nElement of copied list ->", copy_list)
# Removing the last element from the copied list
str_list.remove('XML')
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
# UPdating the first element of the original list
int_list[2] = 4
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
PYTHON LIST SHALLOW COPY : Output
Element of original list -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'XML']]
Element of copied list -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'XML']]
Element of original list -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java']]
Element of copied list -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java']]
Element of original list -> [400, 200, 600, 100, 150, 500, [1, 2, 4], ['Python', 'Java']]
Element of copied list -> [400, 200, 600, 100, 150, 500, [1, 2, 4], ['Python', 'Java']]
In the above example, we can see that any change in one list has the impact on other.
In the above example, we have used two smaller list one of integer type and other is of string type. Instead of changing original or copied list we have done the changes on the smaller list. Now at any point in time we can check that both the original and copied list have the same elements.
But if we change any primitive data types then copy() will behave like deep copy.
# Copy operation using copy() example 5
import copy
int_list = [1, 2, 5]
str_list = ['Python', 'Java', 'XML']
org_list = [400, 200, 600, 'Excel', 100, 150, 500, int_list, str_list]
print("\nElement of original list ->", org_list)
copy_list = copy.copy(org_list)
print("\nElement of copied list ->", copy_list)
# Removing the last element from the copied list
copy_list.remove('Excel')
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
# UPdating the first element of the original list
org_list[3] = 'PHP'
print("\nElement of original list ->", org_list)
print("\nElement of copied list ->", copy_list)
PYTHON LIST SHALLOW COPY : Output
Element of original list -> [400, 200, 600, 'Excel', 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'XML']]
Element of copied list -> [400, 200, 600, 'Excel', 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'XML']]
Element of original list -> [400, 200, 600, 'Excel', 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'XML']]
Element of copied list -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'XML']]
Element of original list -> [400, 200, 600, 'PHP', 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'XML']]
Element of copied list -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'XML']]
In the above example, we can check that the original and copied list are not in sync. So, deep copy has been done.

RELATED TOPICS:
- INTRODUCTION TO PYTHON LISTS
- ACCESSING PYTHON LISTS
- ADDING NEW ELEMENTS IN THE LISTS
- UPDATING PYTHON LISTS
- REMOVING ELEMENT IN THE LISTS
- PYTHON LIST SLICING OPERATION
- USING STEP SIZE IN LIST SLICING OPERATION
- LIST SORTING OPERATIONS
- PYTHON LIST BUILT-IN FUNCTIONS
- REMOVING DUPLICATES USING PYTHON SET OPERATOR
- PYTHON LIST DEEP COPY
Pingback: INTRODUCTION TO PYTHON SETS - Sayantan's Blog On Python Programming