Python Forum
Representation of recursive list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Representation of recursive list
#1
Guess you have following code:
x = [1, 2, 3]
x.append(x)
print(x)
The output:

Did you know/expect that?

It's infinite, because it refers on itself.
You can do this:
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#2
Cool not seen that before.
pprint gives some more info.
λ ptpython
>>> from pprint import pprint

>>> x = [1, 2, 3]
>>> x.append(x)
>>> x
[1, 2, 3, [...]]

>>> pprint(x)
[1, 2, 3, <Recursion on list with id=88766544>]

>>> x[-1]
[1, 2, 3, [...]]
>>> x == x[-1]
True
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a future project: recursive file list Skaperen 0 2,270 Dec-14-2017, 03:55 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020