Remove from one list impact it's copy [python 2.7] [duplicate]












-2
















This question already has an answer here:




  • How to clone or copy a list?

    19 answers




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.











share|improve this question















marked as duplicate by jpp list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 9:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • I just got a -1 w/o any comment - That's not helpful. If you think that question is irrelevant, wrong or useless - please explain (or tag accordingly)

    – DarkLight
    Nov 15 '18 at 8:50
















-2
















This question already has an answer here:




  • How to clone or copy a list?

    19 answers




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.











share|improve this question















marked as duplicate by jpp list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 9:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • I just got a -1 w/o any comment - That's not helpful. If you think that question is irrelevant, wrong or useless - please explain (or tag accordingly)

    – DarkLight
    Nov 15 '18 at 8:50














-2












-2








-2









This question already has an answer here:




  • How to clone or copy a list?

    19 answers




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.











share|improve this question

















This question already has an answer here:




  • How to clone or copy a list?

    19 answers




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.






This question already has an answer here:




  • How to clone or copy a list?

    19 answers








python-2.7 list deep-copy shallow-copy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 13 at 13:19







DarkLight

















asked Nov 15 '18 at 8:23









DarkLightDarkLight

2510




2510




marked as duplicate by jpp list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 9:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by jpp list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 9:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • I just got a -1 w/o any comment - That's not helpful. If you think that question is irrelevant, wrong or useless - please explain (or tag accordingly)

    – DarkLight
    Nov 15 '18 at 8:50



















  • I just got a -1 w/o any comment - That's not helpful. If you think that question is irrelevant, wrong or useless - please explain (or tag accordingly)

    – DarkLight
    Nov 15 '18 at 8:50

















I just got a -1 w/o any comment - That's not helpful. If you think that question is irrelevant, wrong or useless - please explain (or tag accordingly)

– DarkLight
Nov 15 '18 at 8:50





I just got a -1 w/o any comment - That's not helpful. If you think that question is irrelevant, wrong or useless - please explain (or tag accordingly)

– DarkLight
Nov 15 '18 at 8:50












1 Answer
1






active

oldest

votes


















1














You can use in-built copy module.



import copy
a = ['alpha', 'beta', 'gamma', 'delta']
# it will perform the shallow copy
b = copy.copy(a)

b.remove('alpha')

print 'A list is', a
print 'B list is', b


For Python3.x. Though, copy module is available in Python3.x



a = ['alpha', 'beta', 'gamma', 'delta']
b = a.copy()

b.remove('alpha')

print('A list is', a)
print('B list is', b)


Hope this helps






share|improve this answer
























  • Thank you, while I still don't understand why this is the expected behavior - it gives me a solution.

    – DarkLight
    Nov 15 '18 at 14:04


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can use in-built copy module.



import copy
a = ['alpha', 'beta', 'gamma', 'delta']
# it will perform the shallow copy
b = copy.copy(a)

b.remove('alpha')

print 'A list is', a
print 'B list is', b


For Python3.x. Though, copy module is available in Python3.x



a = ['alpha', 'beta', 'gamma', 'delta']
b = a.copy()

b.remove('alpha')

print('A list is', a)
print('B list is', b)


Hope this helps






share|improve this answer
























  • Thank you, while I still don't understand why this is the expected behavior - it gives me a solution.

    – DarkLight
    Nov 15 '18 at 14:04
















1














You can use in-built copy module.



import copy
a = ['alpha', 'beta', 'gamma', 'delta']
# it will perform the shallow copy
b = copy.copy(a)

b.remove('alpha')

print 'A list is', a
print 'B list is', b


For Python3.x. Though, copy module is available in Python3.x



a = ['alpha', 'beta', 'gamma', 'delta']
b = a.copy()

b.remove('alpha')

print('A list is', a)
print('B list is', b)


Hope this helps






share|improve this answer
























  • Thank you, while I still don't understand why this is the expected behavior - it gives me a solution.

    – DarkLight
    Nov 15 '18 at 14:04














1












1








1







You can use in-built copy module.



import copy
a = ['alpha', 'beta', 'gamma', 'delta']
# it will perform the shallow copy
b = copy.copy(a)

b.remove('alpha')

print 'A list is', a
print 'B list is', b


For Python3.x. Though, copy module is available in Python3.x



a = ['alpha', 'beta', 'gamma', 'delta']
b = a.copy()

b.remove('alpha')

print('A list is', a)
print('B list is', b)


Hope this helps






share|improve this answer













You can use in-built copy module.



import copy
a = ['alpha', 'beta', 'gamma', 'delta']
# it will perform the shallow copy
b = copy.copy(a)

b.remove('alpha')

print 'A list is', a
print 'B list is', b


For Python3.x. Though, copy module is available in Python3.x



a = ['alpha', 'beta', 'gamma', 'delta']
b = a.copy()

b.remove('alpha')

print('A list is', a)
print('B list is', b)


Hope this helps







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 8:48









Srce CdeSrce Cde

1,184511




1,184511













  • Thank you, while I still don't understand why this is the expected behavior - it gives me a solution.

    – DarkLight
    Nov 15 '18 at 14:04



















  • Thank you, while I still don't understand why this is the expected behavior - it gives me a solution.

    – DarkLight
    Nov 15 '18 at 14:04

















Thank you, while I still don't understand why this is the expected behavior - it gives me a solution.

– DarkLight
Nov 15 '18 at 14:04





Thank you, while I still don't understand why this is the expected behavior - it gives me a solution.

– DarkLight
Nov 15 '18 at 14:04





Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly