Maximum Python Size: Range (); dynamic or static?

I am new to python, so I am running my usual Project Euler project to develop logical breaks in my head.

Basically, I need a maximum list size, i.e. range (1, n), without overflow.

Any ideas?

+3
source share
2 answers

Look at get_len_of_range and get_len_of_range_longs in the built-in module source

Summary: You will receive an OverflowError if there are more items in the list than can be placed in the signed one for a long time. On 32-bit Python, which 2**31 - 1, and on 64-bit Python, which 2**63 - 1. Of course, you will get a MemoryError even for the values ​​under this.

+5
source

The size of your lists is limited only by your memory. Please note that depending on your version of Python range(1, 9999999999999999), only a few bytes of RAM are required, since it always creates only one virtual list item that it returns.

If you want to instantiate a list, use list(range(1,n))(this will copy the virtual list).

0
source

Source: https://habr.com/ru/post/1754639/


All Articles