0

I played around in the python intepreter and found this strange thing:

>>> a = list()
>>> a.append(a)
>>> print(repr(a))
[[...]]
>>> print(repr(a[0]))
[[...]]
>>> print(repr(a[0][0][0][0]))
[[...]]

Are these lists infinitly reocurring?

What are some practical use cases for these recursive lists?

totokaka
  • 2,244
  • 1
  • 21
  • 33
  • 1
    You have stored a reference to the list itself in the list. **There is only one list**. – Martijn Pieters Mar 24 '14 at 18:05
  • I can see absolutely none practicality in that code :D – Paulo Bu Mar 24 '14 at 18:05
  • @MartijnPieters Yeah, that was what I thought, but as PauloBu asks with me: Are there any use-cases for this? Why is it even possible? – totokaka Mar 24 '14 at 18:15
  • Why wouldn't it be? You can put anything you want into a list. – RemcoGerlich Mar 24 '14 at 18:19
  • Oh, this was a duplicate. The other question wasn't very SEOed. At least when not searching for "[...]".. – totokaka Mar 24 '14 at 18:21
  • The use case is in effect the same as the use-case for `foo.bar = foo`. That is to say, you might some day come up with a situation where you're describing a potentially self-referencing data structure such as a cyclic graph (in this case it would need a *very* short cycle since there's an edge with the same node at both ends, which isn't normally how graphs are defined). Furthermore, you are for some reason representing that structure using just lists of edges. It's possible because it would take time and resources to prevent it, and preventing it would help nobody but harm some. – Steve Jessop Mar 24 '14 at 18:22

0 Answers0