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
6
votes
2 answers

gnuplot - Does `set xrange [x_min:x_max]` limit the ranged used for function fit?

Simple question - the range drawn on a plot can be changed with the set xrange [x_min:x_max] command. Does this command also limit the range used when fitting a function using the data fitting tools in gnuplot? Is there a way to manually specify the…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
6
votes
1 answer

Reason why xrange is not inheritable in Python?

I was trying to inherit xrange (to implement membership testing with in, as well as iteration). But I got the following error message: TypeError: Error when calling the metaclass bases type 'xrange' is not an acceptable base type What's special…
Frozen Flame
  • 3,135
  • 2
  • 23
  • 35
5
votes
2 answers

Can I get the values used to construct a Python 2.7 xrange from the object itself?

If s is a slice object in Python, constructed using either s = slice(start, stop, step) or (in the appropriate context) start:stop:step, the values used to construct s are available from the slice object itself as s.start, s.stop and s.step. Similar…
user200783
  • 13,722
  • 12
  • 69
  • 135
4
votes
8 answers

PYTHON - finding the maximum of every 10 integers in an array

I have a large array of integers, and I need to print the maximum of every 10 integers and its corresponding index in the array as a pair. ex. (max_value, index of max_value in array) I can successfully find the maximum value and the corresponding…
nikki_c
  • 117
  • 1
  • 3
  • 7
4
votes
2 answers

About memory efficiency: range vs xrange, zip vs izip

I was reading the following topic: Make dictionary from list with python The initial problem is to transform the tuple (1,'a',2,'b',3,'c') into the dictionary {1: 'a', 2: 'b', 3: 'c'}. Many interesting solutions were given, including the following…
usual me
  • 8,338
  • 10
  • 52
  • 95
4
votes
2 answers

Accessing xrange internal structure

I'm trying to use ctypes to extract data from internal python structures. Namely, I'm trying to read the 4 fields in an xrange: typedef struct { PyObject_HEAD long start; long step; long len; } rangeobject; Is there any…
UsAaR33
  • 3,536
  • 2
  • 34
  • 55
4
votes
2 answers

Portable, memory efficient range() for Python 2.x and Python 3.x

I am aware of the downsides of range in Python 2.x (it creates a list which is inefficient for large ranges) and it's faster iterator counterpart xrange. In Python 3.x however, range is an iterator and xrange is dropped. Is there a way to write…
dmg
  • 7,438
  • 2
  • 24
  • 33
4
votes
3 answers

PYTHON : Simple random generation driving if/else

new to programmation, im learning and here is probably a very simple problem for you guy. import random def run_stair_yes(): print "\nRunning in stairs is very dangerous!" print "Statistique shows that you have 70% chance of falling" …
inick
  • 33
  • 1
  • 1
  • 5
3
votes
2 answers

Python xrange with float

Beginner programmer here. Is there a way to use float values within the bounds of xrange as well as the step value? I have seen a few solutions for stepping with a float but not for the actual bounds. Essentially I would like to create a loop that…
micuzzo
  • 165
  • 3
  • 12
3
votes
1 answer

xrange versus itertools.count Python 2.7

I want to run a range from a start to an end value. It works fine on low numbers but when it gets too large it causes an overflow error as int too large to convert to C Long. I am using Python 2.7.3. I read here OverflowError Python int too large to…
Python_newbie
  • 133
  • 1
  • 2
  • 7
3
votes
3 answers

Generationg a list from user-input dynamically

As the title says, I'm processing some command-line options to create a list from user input, like this: "3,28,2". This is what I got so far: >>> rR = "3,28,2" >>> rR = re.split(r"[\W]+", rR) >>> map(int, xrange( int(rR[0]),int(rR[1]),int(rR[2])…
MacUsers
  • 2,091
  • 3
  • 35
  • 56
2
votes
3 answers

gnuplot: plot data from one month ago to now

everyone. I have some data which periodically updated. For example: 1330347541 79 100 6 163 38 1330349341 80 103 6 165 38 1330351141 80 104 6 166 40 1330352941 80 104 6 166 40 1330354741 81 104 8 167 41 I want to draw that data on a graph with…
lollo
  • 2,289
  • 16
  • 20
2
votes
2 answers

Gnuplot: how to set xrange with set xtics time?

I plot a time-series of datas (images, throughput), using this: set term postscript color eps enhanced 22 set encoding utf8 set output "tput.eps" load "../styles.inc" set size 1,0.6 set bmargin 4.5 set tmargin 2.5 set lmargin 9 set rmargin 8 set…
Valerio Schiavoni
  • 1,373
  • 1
  • 12
  • 19
2
votes
1 answer

Highcharts xrange bar height 0 for larger data sets

I have an xrange graph where each 'Agent' has a series of Timeline Data, I am having several problems getting the out of the box attributes to format the graph data appropriately. For instance, when there are many Series / Y categories the bar…
Poncher
  • 53
  • 6
2
votes
2 answers

Add an arbitrary element to an xrange()?

In Python, it's more memory-efficient to use xrange() instead of range when iterating. The trouble I'm having is that I want to iterate over a large list -- such that I need to use xrange() and after that I want to check an arbitrary element. With…
Newb
  • 2,810
  • 3
  • 21
  • 35