We know this in Python
a= [1, 2]
b= [a, a]
print b
[[1, 2], [1, 2]]
a.append(3)
print b
[[1, 2, 3], [1, 2, 3]]
This is because b
have reference of same list a
and appending anthing in a
also changes b
.
My question is -- What is the use or advantage of this behavior?
Why Python assign list values by reference??
If I am doing b =[a,a]
obusely it means I am interested in values of a
not in what happens to a
later on.
We can achieve this as
b=[a[:], a[:]]
#or
b=[a,a]
a=[]
but Why this is not the default behavior in python? Why Python is designed like this!!