0

Can someone please help explain why I am unable to print

>>> list(range(4**4**4)).

I am receiving an error OverflowError: range() result has too many items

2 Answers2

0

According to official docs:

https://docs.python.org/2/library/sys.html#sys.maxsize https://docs.python.org/3/library/sys.html#sys.maxsize

sys.maxsize 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.

Try this code to check whether you would be able to create list of your size:

>>> import sys
>>> sys.maxsize > 4**4**4

and try this to verify how python resolves powers in your case:

>>> print(2**2**3)
256
>>> print(2**(2**3))
256
>>> print((2**2)**3)
64
Juan Kania-Morales
  • 558
  • 1
  • 7
  • 13
0
  • You will get an OverflowError if the list has more elements than can be fit into a signed long:

    • On 32bit Python: 2**31 - 1
    • On 64 bit Python: 2**63 - 1
    • You can get a MemoryError even for values just under that.

    For example your number is very big:

    • 2**64 = 18446744073709551616
    • 4**4**4 = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096
Razvan I.
  • 239
  • 1
  • 5