Issue with list clear() function

a=list()
b= [1,2]
c=list()

a.append(b)
c.append(a)
a.clear()

print(b)

This gives me “[]” . Instead of clear del works fine.

Py 3.7.3

Hi Paul, you wrote:

“”"
This gives me “[]” . Instead of clear del works fine.
“”"

I think you are mistaken about that.

>>> a=list()
>>> b= [1,2]
>>> c=list()
>>> 
>>> a.append(b)
>>> c.append(a)
>>> a.clear()
>>> 
>>> print(b)
[1, 2]

My guess is that you actually ran some different code, and instead of
copying and pasting, you tried to recreate it in your post by retyping.

2 Likes

You just cleaned all elements in array a, not b. It’s correct.
Because what you appended to a is just a pointer of array b.
It will not clean array b .
I think what you wanne try just deep del likes deep copy