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
0
votes
1 answer

Can highcharts xrange labels be shown only if they fit the box?

I would like to create a Highcharts xrange chart where only data labels that fit into boxes/ranges are shown. Is this possible? At the moment, labels don't overlap but are shown outside the box when they don't fit. I tried playing with the…
mdomnita
  • 348
  • 2
  • 8
0
votes
0 answers

Highcharts xAxis datetime on xrange graph

I'm learning how to use highcharts and I have an issue with the xrange type. I'm trying to display a simple graph in a div on my website, however I can't see the graph completely. I have an extremely hard time with datetimes... Isn't it supposed to…
user9892254
0
votes
1 answer

Generate list of numbers

Hi I want to Generate list of numbers from 1000000 to 2000000 but the problem is that I get an error memory error I was using random everything was good only I get dublcated number and I cant have duplcated number so i switched to xrange data =…
Mike
  • 15
  • 1
  • 5
0
votes
1 answer

xrange vs iterators python

I am a little confused by why I can't treat an xrange() object as an iterator: In [47]: xr = xrange(1,7) In [48]: next(xr) ----------------------------------------------------------------------- ---- TypeError …
user2399453
  • 2,930
  • 5
  • 33
  • 60
0
votes
1 answer

using xrange output not expected

s = 0 for s in xrange(0, 100): print "s before", s if s % 10 == 0: s += 10 print "s after", s s = 0 while s < 100: print "s before", s if s % 10 == 0: s += 10 s += 1 print "s after", s As pictures…
Sam Y
  • 52
  • 5
0
votes
1 answer

Normalize columns in pandas data frame while once column is in a specific range

I have a data frame in pandas which contains my Experimental data. It looks like this: KE BE EXP_DATA COL_1 COL_2 COL_3 ... 10 1 5 1 2 3 9 2 . . . . 8 3 . . 7 4 6 5 . . The…
NorrinRadd
  • 545
  • 6
  • 21
0
votes
1 answer

Using xrange to Reduce Memory Usage, For Loop

I've got a line of code that is producing a pretty huge memory drain. Here is the line: X_train = (np.array(a).reshape(1000,100) for a in X_train) Simply put, I'm reshaping each row of my dataset. The problem is, this creates a memory error that…
Ashley O
  • 1,130
  • 3
  • 21
  • 34
0
votes
1 answer

Any better way to calculate product of multiple lists?

I need to get all possible combinations based on n-number of input lists and do some stuff to them. current code example: import itertools # example inputs list_small = [1, 2, 3] list_medium = [444, 666, 242] list_huge = [1680, 7559, 5573, 43658,…
Ivan Bilan
  • 2,379
  • 5
  • 38
  • 58
0
votes
0 answers

highcharts highstock xrange large data

I try display with Highcharts-Xrange 70,000 measuring points. However, after a few hundred no longer a graph is drawn I work with C #, Ajax, Java The project itself is unfortunately several hundred Mb big, I put a small data extract the Data file…
MMario
  • 1
  • 2
0
votes
1 answer

Odd Python zip() behavior

I am working with generators in Python, and I am getting odd behavior. I am not entirely how to phrase the question... My code performs a pattern search over an objective function. The issue I see shows up when I zip the results of my patternSearch…
abatea
  • 81
  • 1
  • 8
0
votes
2 answers

Objects of type xrange

When I'm reading xrange reference, it says like this.. Objects of type xrange are similar to buffers in that there is no specific syntax to create them, but they are created using the xrange() function. They don’t support slicing, concatenation or…
Elona Mishmika
  • 480
  • 2
  • 5
  • 21
0
votes
1 answer

How to remove spaces from a xrange list - Python

Lets say I have: hello = list(xrange(100)) print hello The result is: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... I need to remove all those spaces from the list so it can be like: 1,2,3,4,5,6,7,8,9,10...
ney
  • 3
  • 1
0
votes
1 answer

Django view/method called repeatedly without any actual calls

for i in xrange(1,NUM_USERS+1): print i private = RSA.generate(3072,Random.new().read) public = private.publickey() new_user = User(public_rsa=public.exportKey(), secret_rsa=private.exportKey()) …
bawejakunal
  • 1,678
  • 2
  • 25
  • 54
0
votes
2 answers

Python best practices xrange yield

def list_step(max = 268): """ split in steps of 120 elements at time """ if max > 120: for i in xrange(0,max,120): if i <= max: rest = max % xrange(0,max,120)[-1] if rest != 120: …
user2239318
  • 2,578
  • 7
  • 28
  • 50
0
votes
2 answers

Why does Python say "Inappropriate Argument Type" for xrange?

My code is: def euler7(): prime=1 val=0 for num in xrange(3, 9999999999, 2): for n in xrange(1, num): if num%n==0: numb=0 break else: numb=1 …