Python Forum
Why does pop() removes an element from more than one list? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why does pop() removes an element from more than one list? (/thread-22530.html)



Why does pop() removes an element from more than one list? - albufork - Nov-16-2019

I have just started using python and encountered this problem. I made list2 a copy of list1. When I remove the last element ("b") in list2 using list2.pop(), the same thing happens to list1 at the same time (which was not intended). Are list1 and list2 somehow 'entangled'?? What should I do if I want to make such changes to one of the lists only?

I know this might be a silly question but still please help :)

list1 = ["a","b"]
list2 = list1
print("list1 = ", list1)
list2.pop()
print("list1 = ", list1)



RE: Why does pop() removes an element from more than one list? - Gribouillis - Nov-16-2019

After the statement list2 = list1, the two lists are the very same object. In fact there is only one list with two names, list1 and list2. The statement simply creates a new name for the object currently named list1. If you want a new object, make a copy
list2 = list(list1)



RE: Why does pop() removes an element from more than one list? - buran - Nov-16-2019

(Nov-16-2019, 04:52 PM)albufork Wrote: I made list2 a copy of list1.
Actually list1 and list2 point to same object. the assignment just copies the reference to the list, not the actual list, so both list1 and list2 refer to the same list after the assignment.
to make a copy of list use

list2 = list1.copy()
or

list2 = list1[:]
you can also use copy.copy() and copy.deepcopy() from copy module


RE: Why does pop() removes an element from more than one list? - albufork - Nov-17-2019

Thank you, Gribouillis and buran! The problem is now solved!

Now I understand that list1 and list2 point to the same object. Is this the case only in defining a new list but not other instances of objects?


RE: Why does pop() removes an element from more than one list? - Gribouillis - Nov-17-2019

albufork Wrote:Is this the case only in defining a new list but not other instances of objects?
It is the same for all python objects. The effect of the statement spam = eggs is that from now on, the word spam refers to the object that is currently referred to by the name eggs. No new object is created by this statement.


RE: Why does pop() removes an element from more than one list? - buran - Nov-17-2019

In addition, the effect you see on list is because lists are mutable objects. i.e. when you append, pop, etc. it's still the same object, so the change is "visible" in all names that refer to same object. Same will apply to any mutable objects, e.g. dicts
>>> spam = {1:'a', 2:'b'}
>>> eggs = spam
>>> spam[3] = 'c'
>>> spam
{1: 'a', 2: 'b', 3: 'c'}
>>> eggs
{1: 'a', 2: 'b', 3: 'c'}
just for example, strings on other hand are immutable. i.e. you cannot change the object
>>> spam = 'foo'
>>> eggs = spam
>>> id(spam)
140509962431992
>>> id(eggs)
140509962431992
>>> spam = spam + 'bar'
>>> spam
'foobar'
>>> eggs
'foo'
>>> id(spam)
140509962432160
>>> id(eggs)
140509962431992