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")



Output


Picture showing the output of shallow copy in python



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")



Output


Picture showing the output of deep copy in python



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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250