An object
A range()
calculates numbers on demand, for example. when repeating or trying to access certain indices:
>>> r = range(2, 80, 3) >>> len(r) 26 >>> r[15] 47 >>> 42 in r False >>> r[:10] range(2, 32, 3)
This is a sequence because the object supports membership testing, indexing, slicing, and has length, just like a list or tuple. But, unlike a list or tuple, it actually does not contain integers in a sequence in memory, making it virtual.
When you call list()
on a range()
object, you create a new sequence containing all the integers that are in the range, but now you save all these integers:
>>> r_list = list(r) >>> r_list [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77]
This is also a sequence, but it takes up more memory, since all integers are now created in front, and you will use them or not. Thus, a list or tuple is a specific sequence.
Using the sys.getsizeof()
function, we can calculate how much memory each object uses:
>>> import sys >>> sys.getsizeof(r) 48 >>> sys.getsizeof(r_list) + sum(sys.getsizeof(i) for i in r_list) 1072
The list object uses 22 times memory; because it contains 26 whole objects.
To answer the comment on your question, range()
objects are not iterators . Iterators produce values ββone by one on demand, but cannot be indexed, they produce all values ββonly once, and they cannot be indexed. You can create an iterator from a range()
object using the iter()
function:
>>> iter(r) <range_iterator object at 0x10aea23f0> >>> r_iter = iter(r) >>> len(r_iter) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'range_iterator' has no len() >>> list(r_iter) [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77] >>> list(r_iter) []
but after exhaustion, the iterator will not play the same range again.
All of the above applies mainly to Python 3; in Python 2, the type is called xrange()
, where it is more limited in its capabilities (it does not support slicing and can process integers < sys.maxint
).