-2

Is there a maximum amount of levels of nesting lists in Python?

I've combed the documentation over and can't seem to find a direct answer on this. I have found in the SYS documentation (https://docs.python.org/3.8/library/sys) that the default level of recursive calls is 1000, but I'm not sure if this effects nested lists also.

  • Why not try it? If there is a limit you'll hit it in a few seconds. My hypothesis is you can nest until you run out of memory. – MatthewMartin Jan 11 '21 at 18:11
  • Surely it'd just be the memory limit. Recursion of calls has nothing to do with recused lists. It's just binary data. If you can't find any answers online, why not just try it yourself? – Random Davis Jan 11 '21 at 18:12
  • 2
    May I ask *why* you’d potentially need to use the max limit? Surely this suggests a ‘better’ data structure is required. – S3DEV Jan 11 '21 at 18:12

3 Answers3

2

There is no maximum level. Lists just store references to objects. They don't care what those objects are, and there is no physical nesting involved.

You can have self-referential lists, or lists contained in multiple other lists, or all sorts of other stuff that wouldn't work with physical nesting. It's not like, say, a NumPy array, where the array is aware of its dimensionality and there actually is a dimension limit.

user2357112
  • 260,549
  • 28
  • 431
  • 505
1

This is the test I made:

test = []
current = test
while True:
    current.append([])
    current = current[0]

It's been running for over a minute without crashing so I assume it will run until there is no more available memory

Mick
  • 796
  • 4
  • 8
  • Perhaps monitor memory usage via the OS’s tool(s). – S3DEV Jan 11 '21 at 18:24
  • I let it run in Task Manager until it reached 10,000 mb, then I stopped it so my PC wouldn't crash – Mick Jan 11 '21 at 18:25
  • the append function add an element to the end of the list, it doesn't nest the lists. and the last line overrides the variable current and leaves it as an empty list ever turn around the while loop. This would certainly not crash, but it wouldn't answer any questions about nested lists – CatalunaLilith Jan 11 '21 at 22:54
  • @CatalunaLilith You're wrong, test the code in a ranged loop and print the result. – Mick Jan 11 '21 at 22:56
  • @Mick that's exactly what I did, both the code as-is which would go on infinitely, so that I did a Keyboard Interupt on, and now again in a for loop of a range(100). `print(current)` definitely prints off an empty list `[]`, not nested empty lists `[[]]`, etc – CatalunaLilith Jan 12 '21 at 00:04
  • @CatalunaLilith current is just a pointer to the nested list, you have to print test – Mick Jan 12 '21 at 00:05
  • @Mick test does indeed nest. Ironically, printing test (from the while True loop, not a for loop) DOES cause a recursion error "RecursionError: maximum recursion depth exceeded while getting the repr of an object" which points to a maximum object depth that would limit the maximum nesting of lists – CatalunaLilith Jan 12 '21 at 21:30
  • @CatalunaLilith Yea I noticed that too, I wasn't aware print() affects the execution stack. I'm not sure what the difference is, but if you do it without printing it runs forever – Mick Jan 12 '21 at 21:37
1

Test for nest list

current = [1]
count = 0
while True:
    current = [current]
    count += 1
    print(count)
xayam
  • 21
  • 6