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'




Picture showing the output of shallow copy in python

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'




Picture showing the output of deep copy in python

Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250