Why is there no xrange function in Python3?

Recently I started using Python3 and it’s lack of xrange hurts.

Simple example:

  1. Python2:

    from time import time as t
    def count():
      st = t()
      [x for x in xrange(10000000) if x%4 == 0]
      et = t()
      print et-st
    count()
    
  2. Python3:

    from time import time as t
    
    def xrange(x):
    
        return iter(range(x))
    
    def count():
        st = t()
        [x for x in xrange(10000000) if x%4 == 0]
        et = t()
        print (et-st)
    count()
    

The results are, respectively:

  1. 1.53888392448
  2. 3.215819835662842

Why is that? I mean, why xrange has been removed? It’s such a great tool to learn. For the beginners, just like myself, like we all were at some point. Why remove it? Can somebody point me to the proper PEP, I can’t find it.

6 Answers
6

Leave a Comment