for or while loop to do something n times [duplicate]

but on the other hand it creates a completely useless list of integers just to loop over them. Isn’t it a waste of memory, especially as far as big numbers of iterations are concerned?

That is what xrange(n) is for. It avoids creating a list of numbers, and instead just provides an iterator object.

In Python 3, xrange() was renamed to range() – if you want a list, you have to specifically request it via list(range(n)).

Leave a Comment