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))

Comments

Popular posts from this blog

What is composite key?

What are the different data types in Python?

What is __repr__ function?