title: How can I enable xrange in Python 3 for portability?
body: <p>I wrote a script which I wanted to enable for both Python 2 and Python 3.</p>
  
  <p>After importing <code>division</code> and <code>print_function</code> from <code>__future__</code>, my only concern was that my <code>range</code> returns a whole array in Python 2, wasting time and memory.</p>
  
  <p>I added the following 3 lines at the beginning of the script, as a workaround:</p>
  
  <pre><code>if sys.version_info[0] == 3:
      def xrange(i):
          return range(i)
  </code></pre>
  
  <p>Then, I only used <code>xrange</code> in my code.</p>
  
  <p>Is there some more elegant way to do it rather than my workaround?</p>