Recursive lists in Python

Published July 11, 2012

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.