Questions tagged [xrange]

xrange is a Python function that, unlike the traditional 'range' function, creates an iterator object rather than a list.

xrange is a Python function that, unlike the traditional range function, creates an iterator object rather than a list.

Their behaviors are very similar, but behind the scenes it's different.

range returns a list:

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10).__class__
<type 'list'>
>>> for i in range(10):
...     print i,
0 1 2 3 4 5 6 7 8 9

xrange returns an iterable object:

>>> xrange(10)
xrange(10)
>>> xrange(10).__class__ #It's not a list, it's a class of its own.
<type 'xrange'>
>>> for i in xrange(10): #However, it can still be iterated over.
...     print i,
0 1 2 3 4 5 6 7 8 9

While they behave basically the same, xrange works differently. range generates a list and then returns it. xrange, 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.

Questions that include this tag should be related to xrange, not just code that happens to include it. Here are some relevant questions:

Relevant links:

91 questions
842
votes
28 answers

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

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 for i in range(0, 20): for i in xrange(0, 20):
Teifion
  • 108,121
  • 75
  • 161
  • 195
462
votes
12 answers

Should you always favor xrange() over range()?

Why or why not?
mbac32768
  • 11,453
  • 9
  • 34
  • 40
391
votes
6 answers

NameError: global name 'xrange' is not defined in Python 3

I am getting an error when running a python program: Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 110, in File "C:\Program Files (x86)\Wing IDE 101…
Pip
  • 4,387
  • 4
  • 23
  • 31
313
votes
6 answers

Why is there no xrange function in Python3?

Recently I started using Python3 and it's lack of xrange hurts. Simple example: 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() Python3: from time import…
catalesia
  • 3,348
  • 2
  • 13
  • 9
58
votes
3 answers

changing default x range in histogram matplotlib

I would like to change the default x range for the histogram plot. The range of the data is from 7 to 12. However, by default the histogram starts right at 7 and ends at 13. I want it to start at 6.5 and end at 12.5. However, the ticks should go…
Rohit
  • 5,840
  • 13
  • 42
  • 65
53
votes
5 answers

Is there an equivalent of Pythons range(12) in C#?

This crops up every now and then for me: I have some C# code badly wanting the range() function available in Python. I am aware of using for (int i = 0; i < 12; i++) { // add code here } But this brakes down in functional usages, as when I want…
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
29
votes
3 answers

why is xrange able to go back to beginning in Python?

I've encountered this code from Most pythonic way of counting matching elements in something iterable r = xrange(1, 10) print sum(1 for v in r if v % 2 == 0) # 4 print sum(1 for v in r if v % 3 == 0) # 3 r is iterated once. and then it's iterated…
Le Curious
  • 1,451
  • 1
  • 13
  • 13
25
votes
1 answer

Name 'xrange' is not defined in Python 3

I try to execute following code but can't with mistake: name 'xrange' is not defined pages = ( requests.get( build_group_request({ "offset": WINDOW_SIZE * i, "count": WINDOW_SIZE, "fields":…
Jello
  • 275
  • 1
  • 3
  • 3
17
votes
2 answers

How is irange() any different from range() or xrange()?

I was going through Python Generators Wiki when I came across this RangeGenerator page which talks about irange() - This will let us iterator over large spans of numbers without resorting to xrange, which is a lazy list as opposed to a…
nsane
  • 1,715
  • 4
  • 21
  • 31
16
votes
5 answers

`xrange(2**100)` -> OverflowError: long int too large to convert to int

xrange function doesn't work for large integers: >>> N = 10**100 >>> xrange(N) Traceback (most recent call last): ... OverflowError: long int too large to convert to int >>> xrange(N, N+10) Traceback (most recent call last): ... OverflowError: long…
jfs
  • 399,953
  • 195
  • 994
  • 1,670
15
votes
1 answer

Iterating in a closed range [a, b] in python

I want to iterate over a closed range of integers [a, b] in python, ie. iterating from a to b including both a and b. I know the following way of doing it: for i in range(a, b+1): do_something(i) For iterating in the reverse direction (ie. in…
krips89
  • 1,683
  • 4
  • 17
  • 32
12
votes
9 answers

range and xrange for 13-digit numbers in Python?

range() and xrange() work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.
kame
  • 20,848
  • 33
  • 104
  • 159
8
votes
0 answers

Python 3: How is the look up in a range object faster than a look up in a list?

I was scrolling through a lot of list vs. range related questions on StackOverflow when I came across in one of the answers that look up in a range object is a CONSTANT TIME operation! It doesn't make sense how this can be done in O(1), you're going…
ababuji
  • 1,683
  • 2
  • 14
  • 39
8
votes
3 answers

What is faster for loop using enumerate or for loop using xrange in Python?

What is faster, a for loop using enumerate or using xrange? EDIT: I have tested, and I just see minimal differences.
ichigo
  • 317
  • 2
  • 6
  • 17
7
votes
1 answer

How can I enable xrange in Python 3 for portability?

I wrote a script which I wanted to enable for both Python 2 and Python 3. After importing division and print_function from __future__, my only concern was that my range returns a whole array in Python 2, wasting time and memory. I added the…
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
1
2 3 4 5 6 7