What is the difference between the following lines of codes
code1: list1=[1,3,5,6]
list2=list1
code2: list1=[1,3,5,6]
list2=copy.copy(list1)
Code 1 |
Code 2 |
In code 1, No new list is created instead a reference is made to the old list ie., list2 just references list1. |
In code 2, A new copy of list1 is created and assigned to list 2. |
Whenever any change is made to any of the lists, it reflects in the other. |
Any change made to either of the lists is not reflected in the other. |
Learn in your speed, with individual attention - Teachoo Maths 1-on-1 Class