Recursive lists in Python

I discovered by accident today that Python's lists can contain themselves. Python seems to represent this as "[...]".

123456789101112
>>> l = []
>>> l.append(l)
>>> l
[[...]]
>>> l.append(l)
>>> l
[[...], [...]]
>>> l[0]
[[...], [...]]
>>> l[0][0][0][0][0][0][0]
[[...], [...]]
>>> 

Not sure there's a practical use-case for this but it's good to know. Better than crashing the interpreter, I guess.

Talk is cheap

Leave a comment:

HTML is not valid. Use:
[url=http://www.google.com]Google[/url] [b]bold[/b] [i]italics[/i] [u]underline[/u] [code]code[/code]