2

I have written a piece of code and to test it i am calling function named "Comment" for n number of time with some input , where n = 2000000000 but seems to be giving error

Python version = 2.7.6

Piece of code:-

for i in range(2000000000):
   inp = getlis(input1)
   print inp,input2,Comment(inp,input2)
   inp = []

Output:-

Traceback (most recent call last):
  File "cha.py", line 103, in <module>
    for i in range(200000000):
MemoryError

Limit of range in python causing issue.

4
  • Are you going to use i in your loop? Commented Apr 10, 2015 at 9:54
  • leave it , i haven't pasted all the code . main Issue is with range(200..0) @AshwiniChaudhary Commented Apr 10, 2015 at 10:39
  • I mentioned that because there's a better alternative if you're not using the i in your code anywhere. Commented Apr 10, 2015 at 12:28
  • lets assume i donot use variable "i", please provide your alternative answer Commented Apr 13, 2015 at 9:42

1 Answer 1

3

Use xrange:

for i in xrange(2000000000):

This function is very similar to range(), but returns an xrange object instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.