cyclic data structures

J

John Salerno

I'm having some slight trouble understanding exactly why this creates an
infinite loop:

L = [1, 2]
L.append(L)

In a highly abstract way, I understand it. But if I were asked to write
down what I think L is after the second line executes, I would write:

[1, 2, [1, 2]]

But the correct answer seems to be:

[1, 2, [1, 2 [1, 2 [ .......

I guess what confuses me is that there doesn't seem to be anything
inherent in L itself that would cause this repeat. If you add L to L,
why don't you just get two Ls? Where does the cycle begin? Is this just
a quirk that comes with having a variable include a reference to the
same object the variable references?
 
P

Peter Decker

I'm having some slight trouble understanding exactly why this creates an
infinite loop:

L = [1, 2]
L.append(L)

'L' is a pointer to a list. You are now adding that pointer to the
very list it points to.

What you're looking for is:

L = [1, 2]
L.append(L[:])
 
J

John Salerno

Peter said:
'L' is a pointer to a list. You are now adding that pointer to the
very list it points to.

I understand that, but I guess I just don't see how this creates
anything other than a list that refers to [1, 2], and then refers again
to [1, 2], to create [1, 2, [1, 2]].

L.append(L) basically creates this, I think:

[1, 2, L]

Now, assuming that's correct, and since L points to the list [1, 2], why
can't '[1, 2]' be substituted for the 'L' and then the list is closed?
 
F

Fredrik Lundh

John said:
L.append(L) basically creates this, I think:

[1, 2, L]

Now, assuming that's correct, and since L points to the list [1, 2]

L points to [1, 2, L]

</F>
 
C

Carsten Haese

Peter said:
'L' is a pointer to a list. You are now adding that pointer to the
very list it points to.

I understand that, but I guess I just don't see how this creates
anything other than a list that refers to [1, 2], and then refers again
to [1, 2], to create [1, 2, [1, 2]].

L.append(L) basically creates this, I think:

[1, 2, L]

Now, assuming that's correct, and since L points to the list [1, 2], why
can't '[1, 2]' be substituted for the 'L' and then the list is closed?

L is [1,2] before the append. After the append, L is [1,2,L], which is
[1,2,[1,2,L]], which is [1,2,[1,2,[1,2,L]]] etc into infinity. L
literally contains itself after the append.

HTH,

Carsten.
 
D

Duncan Booth

John said:
L.append(L) basically creates this, I think:

[1, 2, L]
Correct.

Now, assuming that's correct, and since L points to the list [1, 2],

wrong. You just said it yourself: L is now the list [1, 2, L]
why
can't '[1, 2]' be substituted for the 'L' and then the list is closed?
The append method modifies the list in place. The list which contained 1,2
now has a third element which is a reference to itself.
 
J

John Salerno

Carsten said:
L is [1,2] before the append. After the append, L is [1,2,L], which is
[1,2,[1,2,L]], which is [1,2,[1,2,[1,2,L]]] etc into infinity. L
literally contains itself after the append.

Ah, this perfectly explains it now! I guess I was dancing around this
fact, but now that you've typed it out, I see that L actually changed in
place!

Thanks guys!
 
E

Ewald R. de Wit

John said:
I'm having some slight trouble understanding exactly why this creates an
infinite loop:

L = [1, 2]
L.append(L)

I tried this with Python 2.3.5 and it handles this tailbiter in a
very pleasantly surprising way:
l = [ 0, 1 ]
l.append( l )
l [0, 1, [...]]
l[2] [0, 1, [...]]
l[2][2][2][2][2][2][2][0]
1

The [...] I have never seen before anywhere, is it documented?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,284
Messages
2,571,411
Members
48,104
Latest member
Hellmary01

Latest Threads

Top