I was going through the Python DOC when I came upon lists and was confused by these :-
1.
>>> a = [1, 2, 3]
>>> b = a
>>> a.append(4)
>>> a
[1,2,3,4]
>>>b
[1,2,3,4]
>>> a = []
>>> print(a)
[]
>>> print(b)
[1, 2, 3, 4]
How can appending to a change both a and b but using a=[]
only changes a and not b.
- As we know
id(a) != id(a[:])
then why doinga[:]=[]
changes a?
Thank you.