0

Simple console sequence:

    >>> A=[]; B=[]
    >>> A.append(1)
    >>> A
    [1]
    >>> A=[]
    >>> A
    []
    >>> B.append(A)
    >>> B
    [[]]

so far so expected. B ist now a list containing one element, which is an empty list. A is an empty list. Let's continue:

    >>> A.append(1)
    >>> A
    [1]
    >>> A=[]
    >>> A
    []

A has the same value as first time. Now the surprise (to me at least):

    >>> B.append(A)
    >>> B
    [[1], []]
    >>>

Why ???? How and where ist the previous content of A, the 1, stored, and why is [1] pre(!!)pended to B instead of another empty list being appended ?

Orla
  • 3
  • 2

1 Answers1

1

You've stumbled on pointers! What's happening is that, each time you declare a new list using =[], a new object is created in memory. Each variable is only "pointing to" that location in memory.

So the sequence of what happened is that you created a list at location x01 and A was pointing at location x01. Then, you appended 1 to that list, so location x01 held an element "1". Then, you created a new list B at x02, which now holds a reference pointing to x01.

When you then overwrote A, you created a new list at x03 and set A to point there. However, B is still pointing at x02, which is still pointing at x01, which still holds a 1! Therefore, when you now append A to B, B is pointing at a location in memory (x02) which has two pointers: one at x01 and one at x03. x01 holds a 1 and x03 is empty.

The bottom line is, be cognizant of when you are creating a new object and when you are editing an existing object - editing an object will not create a new memory location, but creating a new object will. You can get some pretty unintuitive behavior with this if you're not careful.

David_Leber
  • 146
  • 6
  • Hm. I create two lists, A at x01, B at x02. I append 1 to x01. I create a new list at x03, and A now points to it. I append it to B, so x02 now points to x03. We see that when B is displayed as a list containg an empty list. Now i append 1 to A, which is location x03. so B still points to x03, and would (if i had checked) contain a list with one element, 1, the console would display [[1]]. Now i create another empty list at x04. i append that list to B. so i have appended an empty list to the [1] that B was already pointing to. Oh i get it now ! Thank you very much !! – Orla Aug 30 '22 at 21:22