473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

cyclic data structures

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?
Feb 13 '06 #1
8 1517
On 2/13/06, John Salerno <jo******@nospamgmail.com> wrote:
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[:])

--

# p.d.
Feb 13 '06 #2
Peter Decker wrote:
'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?
Feb 13 '06 #3
On Mon, 2006-02-13 at 16:03, John Salerno wrote:
Peter Decker wrote:
'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.

Feb 13 '06 #4
John Salerno wrote:
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>

Feb 13 '06 #5
John Salerno wrote:
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.

Feb 13 '06 #6
Carsten Haese wrote:
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!
Feb 13 '06 #7
Duncan Booth wrote:
The append method modifies the list in place.


That's the key that unlocked the mystery for me. ;)
Feb 13 '06 #8
John Salerno wrote:
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?

--
-- Ewald
Feb 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thomas Mailund | last post by:
Hi group. I have a problem with some C extensions I am working with and hope that some of you can help. Basically, I am wrapping a a tree structure from C where I have python methods for...
0
by: Yves Forkl | last post by:
I need to transform a directed cyclic graph into a DTD. My approach is to have one element Pn for each "production" number n, with the content model comprising the starting vertex number n and the...
7
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in...
9
by: John Doe | last post by:
Hi all, Regarding those cyclic dependencies of classes (like cases in which class A refers to class B and class B to class A): can they ALL be resolved by forward defining classes, and by...
3
by: Dennis Lerche | last post by:
Hi I have a problem regarding cyclic dependency, yeahh I know bad design. But right at this moment I can't see how it should be redesigned to avoid this. The problem is that I just can't get it...
5
by: free2cric | last post by:
Hi, how to detect head and tail in cyclic doubly link list ? Thanks, Cric
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
3
by: mfareed | last post by:
Can somebody help me with the CRC (Cyclic Redundancy Code) algorithm implementation given below: There is a three byte packet header. <--------1st byte-----><--------2nd...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.