Recursive reference to a list within itself
list, python, repr, self-reference
Solution
The difference is only in the way the list is displayed. I.e. the value of `y` is exactly what you'd expect.
The difference in the way the lists are displayed results from the fact that, unlike `l`, `y` is not a self-referencing list:
l[0] is l
=> True
y[0] is y
=> False
`y` is not self-referencing, because `y` does not reference `y`. It references `l`, which is self-referencing.
Therefor, the logic which translates the list to a string detects the potential infinite-recursion one level deeper when working on `y`, than on `l`.
Problem
So I came across something very weird in python. I tried adding a reference to the list to itself. The code might help demonstrate what I am saying better than I can express. I am using IDLE editor(interactive mode). ``` >>>l=[1,2,3] >>>l.append(l) >>>print(l) [1,2,3,[...]] >>>del l[:-1] >>>print(l) [[...]] ``` So far the output is as expected. But when I do this. ``` y=l[:] print(y) ``` To me it seems that the output should be ``` [[...]] ``` But it is ``` [[[...]]] ``` Apparently instead of creating a copy of the list, it puts a reference to the list in y. y[0] is l returns True. I can't seem to find a good explanation for this. Any ideas?