2

So, I have this little bit of code here that I made for fun,

import itertools
list = []
for int in itertools.count():
    list.append(int)
       print list[int]

Now, all of my friends tell me this will stop working at some point. This led me to believe lists can only contain a finite amount of items. Is this true? If so, what is the limit?

Vladimir Putin
  • 661
  • 1
  • 8
  • 18
  • I think you'll run out of memory faster.. – vaultah Jun 28 '14 at 17:39
  • Internal memory? Hard disk free space? Cloud-based storage? The Internet As A Whole? The number of bits that can be stored in the universe? – Jongware Jun 28 '14 at 17:40
  • 1
    Please see the question [http://stackoverflow.com/questions/855191/how-big-can-a-python-array-get] and in particular [this answer](http://stackoverflow.com/a/15739630/831878). – Ray Toal Jun 28 '14 at 17:40

1 Answers1

9

Yes, there is a limit, sys.maxsize is the maximum number of entries a list can contain:

The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • How come the Python 3 documentation for that constant no longer mentions lists, strings or dicts? https://docs.python.org/3/library/sys.html#sys.maxsize – Boris Verkhovskiy Jan 25 '20 at 18:13
  • 2
    @Boris: The Python 2 and 3 branches each had the value added in separate commits, and the messages merely differed. The Python 2 version was added [when the attribute was backported](https://bugs.python.org/issue2488). The wording in Python 2 was used to differentiate it from `sys.maxint`, a value that's entirely *gone* in Python 3, as [`sys.maxsize` replaced it](https://github.com/python/cpython/commit/a37d4c693a024154093b36a612810c3bd72d9254). – Martijn Pieters Jan 25 '20 at 19:51