Articles → Python → Deep copy and shallow copy in list in python

Deep copy and shallow copy in list in python






Shallow copy




list1 = [] 
list2 = []

# shallow copy
list2 = list1.copy()

if (list1 is list2): 
    print("List1 and List2 is having same reference") 
else: 
    print("List1 and List2 is having different reference")





Click to Enlarge


Deep copy




list1 = [] 
list2 = []

# Deep copy
list2 = list1

if (list1 is list2): 
    print("List1 and List2 is having same reference") 
else: 
    print("List1 and List2 is having different reference")





Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, May 17, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250