0

As we know to copy list we have to copy() method, otherwise changes of values could happen. Just like this one:

1st CODE:

list1 = ['a', 'b', 12]
list2 = list1
print('List1: ',list1)
print('List2: ',list2)
list2.append('new_val')
print('Updated_List1: ',list1)
print('Updated_List2: ',list2)

It's O/P:

List1:  ['a', 'b', 12]
List2:  ['a', 'b', 12]
Updated_List1:  ['a', 'b', 12, 'new_val']
Updated_List2:  ['a', 'b', 12, 'new_val']

Above code i got it. BUT IF WE DO LIKE THIS(below code):

2nd CODE:

list1 = ['a', 'b', 12]
list2 = list1
list3 = ['x','y','z']
list2 = list2 + list3
print('List1: ',list1)
print('List2: ',list2)
print('List3: ',list3)

IT's O/P:

List1:  ['a', 'b', 12]
List2:  ['a', 'b', 12, 'x', 'y', 'z']
List3:  ['x', 'y', 'z']

Here you can see, 1st code: changes in list2 affects list1 too. But in 2nd code: it's not happening. Can anyone explain why is this happening or i'm missing something?

3
  • I guess checking the link will give you the solution docs.python.org/3/tutorial/datastructures.html Commented Mar 11, 2022 at 10:57
  • 1
    In your second code: list1, list2 and list3 are three different lists. They're not different references to the same list. Commented Mar 11, 2022 at 10:58
  • 1
    On the contrary, list1 and list2 are references to the same mutable list in first example Commented Mar 11, 2022 at 10:59

2 Answers 2

3

In first code, list2 is just a reference of list1, hence change in list2 affects list1.

Type id(list1) and id(list2), they should be the same in case of first code implying that list1 and list2 have the same address in the memory and they are the same (The id(object) prints the memory location of the object in python).

In the second code, that's not the case. The statment list2 = list2 + list3 creates a new copy of list2 in memory. Checking for id(list1) and id(list2) will be different now.

Maybe checking the docs for copy will be helpful.

Sign up to request clarification or add additional context in comments.

3 Comments

You are absolutely right! 1st code: ids are same for list1&list2. But for 2nd Code: all ids are different. That means if we do list2=list1, it will create reference but we add anything to it afterwards like, list2 = list2+list3, reference part will get over and will create a new variable in memory? Am i right? and why exactly is it happening?
From my understanding of copy, you are right! In list2 = list2 + list3, a new compound object (list2) will be created which does not happen in list2 = list1.
Clearly list2 + list3 creates a new list. How could the resulting list have the appropriate contents if it was not a new list?
0

You can also do

list1 = [ *list2 ]

to copy items of list2 to list1. It creates a new list object having the items of list2 then assigns to list1

N.B: Don't use it for nested lists

2 Comments

It makes a copy, but not a deep-copy. If an element of list2 is a list and the content of that is modified, then it will also be modified in list1.
Thanks, @md2perpe for the clarification

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.