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

python xrange with 0*l to l

this is what am trying to do : l = 4 length = 10**l #10000 for x in xrange(length): password = str(username)+str("%02d"%x) #but here instead of 2 i want it to be l as you can see i want to control the format string with a variable witch i could…
s4my
  • 29
  • 1
  • 9
0
votes
2 answers

How to efficiently select entries by date in python?

I have emails and dates. I can use 2 nested for loops to choose emails sent on same date, but how can i do it 'smart way' - efficiently? # list of tuples - (email,date) for entry in list_emails_dates: current_date = entry[1] for next_entry…
ERJAN
  • 23,696
  • 23
  • 72
  • 146
0
votes
1 answer

Vertical span bands with jqplot

I'd like to add vertical span areas on a jqplot by defining for each one the x axis range. Is there an easy and straightforward way to do this ? I tried setting an overlay with x value set as the centre of the vertical area I want to cover and the…
Demetrius
  • 61
  • 9
0
votes
1 answer

Python for loop and UnboundLocalError

I am a python new comer. Right Now I am having some troubles with python's local variable setting. Here is the thing, I got a mistake output called : UnboundLocalError: local variable 'i1' referenced before assignment By searching on the…
Tk_75963
  • 15
  • 3
0
votes
1 answer

Find position in a csv list

Say I have the following CSV file; 1.41, 123456 1.42, 123456 1.43, 123456 and i want to find the "position"/location of a value in row 0 i.e. "1.41, 1.42, 1.43" in this case, depending on whether the particular row value is greater than or equal to…
user3439187
  • 613
  • 1
  • 7
  • 10
0
votes
2 answers

list of random integers without zero Python

I need a random list of integers without 0. I'm using random.sample(xrange(y),z) but I don't want 0 in this list. Thank you
user3250719
  • 105
  • 1
  • 2
  • 7
0
votes
5 answers

xrange not adhering to start, stop, step

My input file looks like: 6 *,b,* a,*,* *,*,c foo,bar,baz w,x,*,* *,x,y,z 5 /w/x/y/z/ a/b/c foo/ foo/bar/ foo/bar/baz/ When I use xrange, why does it not adhere to the start, stop, step method? with open(sys.argv[1], 'r') as f: for _ in xrange(0,…
David Vasandani
  • 1,840
  • 8
  • 30
  • 53
0
votes
1 answer

range function does not see $i

I am trying to learn range and xrange functionality by plugging PHP.net code into a code generator. When I run the following code I am given the error: unexpected '$i' (T_VARIABLE) on line 4 Here is the code: function xrange($start, $limit,…
Willow
  • 1,040
  • 2
  • 10
  • 21
0
votes
4 answers

xrange generating strings? I don't get it

For some reason, the x variable in the first for... loop changes from int to str after a single iteration through the code. I'm baffled as to why that must be, but each time I run this script, my set ends up being populated by strings consisting of…
Zac Smith
  • 328
  • 1
  • 10
0
votes
2 answers

Increment on certain condition with xrange()

A very short and probably easy to answer question for the ones with more programming experience. I want to increment my counter by one if a certain condition is met. I use xrange() in my for-loop. Can I manually increment i or do I have to build by…
LarsVegas
  • 6,522
  • 10
  • 43
  • 67
-1
votes
1 answer

list out of range when going through an array checking for duplicates in python 2.7

The goal is simple: there is a file with x numbers separated by ` . If there are dublicates of the number in the file they should be deleted. I decided to solve this by comparing the n member with all others from n to len(array). The code: …
-2
votes
1 answer

Python xrange countdown make

For the following code: for slot in xrange(360-1,-1,-1): What does it output ? How do I obtain my desired output of: 360-359-358-357-356....-- 3-2-1
Adem d
  • 21
  • 3
-2
votes
2 answers

How to fix" xrange() arg 3 must not be zero" error in python using parallel programming?

import time from multiprocessing import Process, Pool import sys, os, inspect import urllib import re index ={} graph={} # Common words that we don't want to be part of the index g=['is','a','the','ga','all','to','under'] def rm_tag(data): p =…
-3
votes
1 answer

What is Ruby's equivalent to pythons xrange?

Apparently xrange in python is faster than range. because xrange creates a sequence of objects lazily. However range creates objects in memory. What I'd like to know is what is Ruby's equivalent of pythons xrange?
Jamy
  • 115
  • 1
  • 9
-3
votes
2 answers

reversed() vs. xrange()

I need to traverse a list backwards. I read about xrange() and reversed(). Which one is more expensive?
jon
  • 81
  • 1
  • 2
  • 13