I'm trying to understand the functionality of the clear() function in Python. I noticed that when I clear a list variable that was used to append an array to an existing list variable in Python, it seems to retroactively alter the data placed into the variable.
Take for example the below code:
numbers = [[1, 2, 3]]
newNumberList = [4, 5, 6]
numbers.append(newNumberList)
newNumberList.clear()
I would assume that numbers should contain the values: [[1,2,3], [4,5,6]]
, seeing as the clear() function used on newNumberList
took place after it was appended to numbers.
However,
the value for numbers instead becomes: [[1, 2, 3], []]
, Despite clear() seemingly never interacting with the variable numbers.
Interesting to also note; reinitializing newNumberList instead of using clear() like so:
newNumberList = []
does not alter the variable numbers, and does allow the value of numbers to end up being: [[1, 2, 3], [4, 5, 6]]
I'm learning Python and am 99% sure there is an obvious answer to this that is staring me in the face, but any assistance with this question I have would be greatly appreciated.