11.5. Range and Xrange objects

We have already introduced the range function which generates a list of integers from a start to an end value (the last is not included) with a regular interval. This function is very useful while writing for loops because it gives the possibility to iterate rather over the indices of a collection than over their elements. So you have not only access to the value of the element but also its position in the collection.

Sometimes you need to iterate over a great interval of integers. In this case the list generated by the range function would be long and therefore take a lot of memory space and time to be constructed. In such cases it is more appropriated to use the xrange function which is used in the same way as the range function. But rather than to create the list of integers physically in memory, it creates an object that calculates the new value if it is asked for the next element in the list.