When you do:
l.append(l)
The link to the list is ladded to the list l:
>>> l = [1, 2]
>>> l.append(l)
>>> l is l[2]
True
>>>
In other words, you put the list inside yourself. This creates an endless reference cycle that is presented [...].
To do what you want, you need to add a copy of the list l:
>>> l = [1, 2]
>>> l.append(l[:])
>>> l
[1, 2, [1, 2]]
>>> l is l[2]
False
>>>
source
share