What is the difference between range and xrange functions in Python 2.X?

后端 未结 28 2412
深忆病人
深忆病人 2020-11-22 03:14

Apparently xrange is faster but I have no idea why it\'s faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about

28条回答
  •  無奈伤痛
    2020-11-22 04:03

    range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.

    xrange is a generator, so it is a sequence object is a that evaluates lazily.

    This is true, but 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))
    

提交回复
热议问题