Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>xrange</code> is a Python function that, unlike the traditional <code>range</code> function, creates an iterator object rather than a list. </p> <p>Their behaviors are very similar, but behind the scenes it's different.</p> <p><code>range</code> returns a list:</p> <pre><code>&gt;&gt;&gt; range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] &gt;&gt;&gt; range(10).__class__ &lt;type 'list'&gt; &gt;&gt;&gt; for i in range(10): ... print i, 0 1 2 3 4 5 6 7 8 9 </code></pre> <p><code>xrange</code> returns an iterable object:</p> <pre><code>&gt;&gt;&gt; xrange(10) xrange(10) &gt;&gt;&gt; xrange(10).__class__ #It's not a list, it's a class of its own. &lt;type 'xrange'&gt; &gt;&gt;&gt; for i in xrange(10): #However, it can still be iterated over. ... print i, 0 1 2 3 4 5 6 7 8 9 </code></pre> <p>While they behave basically the same, <code>xrange</code> works differently. <code>range</code> generates a list and <em>then</em> returns it. <code>xrange</code>, typically used for low-memory systems or very large ranges, generates the numbers one at a time, once on each iteration rather than all at once before the loop.</p> <p>Questions that include this tag should be related to <code>xrange</code>, not just code that happens to include it. Here are some relevant questions:</p> <ul> <li><a href="http://stackoverflow.com/questions/94935/what-is-the-difference-between-range-and-xrange">What is the difference between range and xrange?</a></li> <li><a href="http://stackoverflow.com/questions/15409752/strange-xrange-behavior-in-python-2">strange xrange() behavior in Python 2</a></li> </ul> <p>Relevant links:</p> <ul> <li><a href="http://docs.python.org/2/library/functions.html#xrange" rel="nofollow">Python docs</a></li> <li><a href="http://stackoverflow.com/questions/135041/should-you-always-favor-xrange-over-range">Should you always favor xrange() over range()?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload