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.

 

 

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.