I created a copy of a list. When an item was removed from one copy - it was removed from the original as well.
a = ['alpha', 'beta', 'gamma', 'delta']
b = a
b.remove('alpha')
print 'A list is', a
print 'B list is', b
How should I create an independent copy of the list, that will not impact the original?
Late addition
To understand the reason for this mistake - one should refer to the difference between Shallow Copy and Deep Copy Python documentation - 8.17. copy
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
- A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
- A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.