The size in memory of an object can be gotten with sys.getsizeof.
As one could expect, the size of [] is smaller than the size of [[]]. On my machine, I get the following sizes:
>>> sys.getsizeof([])
36
>>> sys.getsizeof([[]])
40
Now, whatever the number of nested empty lists I have, I always get the same size:
>>> sys.getsizeof([[[]]])
40
>>> sys.getsizeof([[[[]]]])
40
What is the reason why the size of nested empty lists seems to have an upper boundary?
Reading the documentation would have taught me that when calling
getsizeof,Since
[]is a container, its size, according togetsizeof, is its own size plus the size of the references it contains, but not the sizes of the objects referred to.Therefore, if
[]has a size of36, and a reference has a size of4, then the size of[[]]is36+4, hence40.Now,
[[[]]]is nothing more than[x]wherexis a reference to[[]]. Hence, the size of[[[]]]is the size of[]plus the size of a reference, so40as well.