Difference between range and xrange
range vs xrange :
-
range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.
-
xrange is a sequence object that evaluates lazily.
-
xrange is not exactly a generator but it evaluates lazily and acts like a generator.
-
In Python 3, range will be implemented by the Python 2 xrange(). If you need to actually generate the list, you will need to do:
list(range(1,100))
range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.
xrange is a sequence object that evaluates lazily.
xrange is not exactly a generator but it evaluates lazily and acts like a generator.
In Python 3, range will be implemented by the Python 2 xrange(). If you need to actually generate the list, you will need to do:
Comments
Post a Comment