473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

using range() in for loops

I'm reading Text Processing in Python right now and I came across a
comment that is helping me to see for loops in a new light. I think
because I'm used to the C-style for loop where you create a counter
within the loop declaration, for loops have always seemed to me to be
about doing something a certain number of times, and not about iterating
over an object.

The reason for this distinction comes from the fact that I read a lot
how using range and for is somewhat discouraged, because it doesn't
really use a for loop for it's true purpose. So my question is, is this
just a Python-oriented opinion about for loops, or is it a general idea?

Also, what if you *do* need to just do something a set number of times.
Is this okay, or does it mean you are approaching the problem
incorrectly? Using for and range together seems to be a common idiom,
yet at the same time discouraged, so I'm wondering what is a good balance.

Thanks.
Apr 5 '06 #1
17 3024
John Salerno <jo******@NOSPAMgmail.com> writes:
The reason for this distinction comes from the fact that I read a lot
how using range and for is somewhat discouraged, because it doesn't
really use a for loop for it's true purpose. So my question is, is
this just a Python-oriented opinion about for loops, or is it a
general idea?


Normally you'd use range or xrange. range builds a complete list in
memory so can be expensive if the number is large. xrange just counts
up to that number.
Apr 5 '06 #2
hi John,
Python doesn't provide for loop like C / C++ but using Range() or
Xrange() you can achive all the functionalities of the C for loop.If
you wants distributed for loop You can use Xrange.
John Salerno wrote:
I'm reading Text Processing in Python right now and I came across a
comment that is helping me to see for loops in a new light. I think
because I'm used to the C-style for loop where you create a counter
within the loop declaration, for loops have always seemed to me to be
about doing something a certain number of times, and not about iterating
over an object.

The reason for this distinction comes from the fact that I read a lot
how using range and for is somewhat discouraged, because it doesn't
really use a for loop for it's true purpose. So my question is, is this
just a Python-oriented opinion about for loops, or is it a general idea?

Also, what if you *do* need to just do something a set number of times.
Is this okay, or does it mean you are approaching the problem
incorrectly? Using for and range together seems to be a common idiom,
yet at the same time discouraged, so I'm wondering what is a good balance.

Thanks.


Apr 5 '06 #3
John Salerno wrote:
The reason for this distinction comes from the fact that I read a lot
how using range and for is somewhat discouraged, because it doesn't
really use a for loop for it's true purpose. So my question is, is this
just a Python-oriented opinion about for loops, or is it a general idea?
The use of range in for loops is not discouraged, just the unnecessary
use of it. In many languages, you were forced to use range - or size,
or length, or count, etc - because the only practical method of
iteration was to use successive indices into the array or other
collection, stopping when the indexes run out. Most loops operate on
some sort of structure in this way.

In Python, you can iterate directly over the structure instead, so you
no longer have to worry about keeping track of the current index,
comparing it to the maximum index, or collecting the right object from
the structure according to its index. Python just gives you the next
object until there are none left. As a result, for most looping tasks,
you have no need to use range or an equivalent.
Also, what if you *do* need to just do something a set number of times.
Is this okay, or does it mean you are approaching the problem
incorrectly?


Do you really need to do something "a number of times"? If so, range
(or xrange) is perfect for the job. More often though, you need to do
something "once for every <whatever>", so put your 'whatevers' in a
list and iterate over that.

--
Ben Sizer

Apr 5 '06 #4
Ant
It's not just a Python thing, Java for example generally uses the
idiom:

for (Iterator it = list.iterator(); it.hasNext(); ) {
Object next = it.next();
//Do stuff to next
}

Horrible compared to the python idiom of course (though the latest
version supports for (x : list){})

Ruby has something similar in:

list.each do |item|
print item
end

Again, not as nice as the python idiom IMHO (Though there may be better
ways - I don't know Ruby very well).

So really, all modern programming languages seem to prefer the
loop-over-iterator idiom in preference to looping over a numerical
range.

Apr 5 '06 #5
John Salerno schreef:
I'm reading Text Processing in Python right now and I came across a
comment that is helping me to see for loops in a new light. I think
because I'm used to the C-style for loop where you create a counter
within the loop declaration, for loops have always seemed to me to be
about doing something a certain number of times, and not about iterating
over an object.

The reason for this distinction comes from the fact that I read a lot
how using range and for is somewhat discouraged, because it doesn't
really use a for loop for it's true purpose. So my question is, is this
just a Python-oriented opinion about for loops, or is it a general idea?

Also, what if you *do* need to just do something a set number of times.
Is this okay, or does it mean you are approaching the problem
incorrectly? Using for and range together seems to be a common idiom,
yet at the same time discouraged, so I'm wondering what is a good balance.


I felt more or less the same when I first learned Python; I was also
used to C-style loops, coming from a C/C++ background. In the end
though, it turned out to be a non-issue for me.

In many cases loops really are for iterating over sequences; more so
than I realized when using for loops in C or C++. In these cases,
Python's for statement works better than C-style loops. And if you
really need to do something a certain number of times, there's still
range() or xrange() to do it.

It's quite simple, I think:
- You have a sequence or iterator to loop over? Use for x in sequence.
- You want something done a set number of times? Use for i in range().
- You want to loop over a sequence and also need the index? Use for i, x
in enumerate(sequence).

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Apr 5 '06 #6
Roel Schroeven wrote:
In many cases loops really are for iterating over sequences; more so
than I realized when using for loops in C or C++. In these cases,
Python's for statement works better than C-style loops. And if you
really need to do something a certain number of times, there's still
range() or xrange() to do it.


Yeah, I'm starting to see the distinction now. I think one thing that
confused me was that C# had two separate loops for these
functionalities, the for loop and the foreach loop (which is equivalent
to Python's for). But even when just doing something a number of times
(C#'s for, Python's for), it looks much cleaner in Python because you
don't have the long, messy three-part for statement.
Apr 5 '06 #7
Paul Rubin wrote:
Normally you'd use range or xrange. range builds a complete list in
memory so can be expensive if the number is large. xrange just counts
up to that number.


so when range would be used instead of xrange. if xrange is more
efficient, why range was not reimplemented?

Apr 5 '06 #8
su**************@gmail.com wrote:
hi John,
Python doesn't provide for loop like C / C++ but using Range() or
Xrange() you can achive all the functionalities of the C for loop.


Not quite.

Georg
Apr 5 '06 #9
AndyL wrote:
Paul Rubin wrote:
Normally you'd use range or xrange. range builds a complete list in
memory so can be expensive if the number is large. xrange just counts
up to that number.


so when range would be used instead of xrange. if xrange is more
efficient, why range was not reimplemented?


Because of backwards compatibility. range() returns a list, xrange() an
iterator: list(xrange(...)) will give the same results as range(...).

In for loops, using xrange instead of range makes no difference since the
loop only iterates over the range. But it's a problem when someone just
does

l = range(100)

and assumes that he's got a list, probably doing

l.remove(5)

and so on.

In Python 3000, plans are that range() will be the same as xrange() is now,
and anyone needing a list can call list(range(...)).

Georg
Apr 5 '06 #10
On Tue, 2006-04-04 at 21:54 -0400, John Salerno wrote:
I'm reading Text Processing in Python right now and I came across a
comment that is helping me to see for loops in a new light. I think
because I'm used to the C-style for loop where you create a counter
within the loop declaration, for loops have always seemed to me to be
about doing something a certain number of times, and not about iterating
over an object.
This is a normal reaction. Try to keep in mind that when you use any
"higher level" language your code will closer reflect what you want
rather than how to accomplish what you want. When you are creating a
loop over numbers, is your goal really to count, or to use the integer
that you are counting to do something else, like perhaps dereference
elements of a string.

Also, part of the problem is a preconceived restriction that you hold on
the use of forloops. They arn't just for counting, there are all sorts
of interesting things that can go in there. Remember, a for loop is
basically a while loop with a little bit of syntactic sugar. Look at
this:
for( a, b, c ) {
d;
e;
}

is the same as

a;
while (b)
{
d;
e;
c;
}
C for and while are the same creature. Python's while loop is Python's
version of for/while. If you wanted to mimick C you could write

i = 0
while( i<10 ):
print i
i+=1

but this is clumbsy and slower. for(i=0;i<10;i++) is a common enough
programing pattern, you arn't really interested in setting i,
incrementing, doing all of that housekeeping. You really want to repeat
10 times with with i set to 0, 1 ...

for ... xrange does this well and is somewhat the motivation for the
creation of xrange.
The reason for this distinction comes from the fact that I read a lot
how using range and for is somewhat discouraged, because it doesn't
really use a for loop for it's true purpose. So my question is, is this
Nothing in a well defined language has a true purpose. The true purpose
of an int in C isn't to be for loop fodder. One of the hallmarks of a
well designed language is orthogonality; most anything works with
anything else. A feature that has one specific use doesn't provide much
utility to justify the effort used to create it.

just a Python-oriented opinion about for loops, or is it a general idea?
Programming languages borrow from heavily from natural languages; yes,
for is a loaned word from English that exists in C, python ... lots of
languages. But just as when one human language borrows from another,
the resulting semantics are not always the same.

Each as a formal semantic. C and Python are somewhat different, and
yes, you could describe the philosophical difference as a matter of
opinions.

Also, what if you *do* need to just do something a set number of times.
Is this okay, or does it mean you are approaching the problem
incorrectly? Using for and range together seems to be a common idiom,
yet at the same time discouraged, so I'm wondering what is a good balance.


The correct idiom is for( xrange( foo )). This is preferred over range
for efficiency.

Historically there were no iters. If you wanted to do loop you would
say

for x in range( 10 ):
foo
bar

range created a list of 10 items and x marched over them. Creating this
list in advance doesn't need to take any more time; you have to create
the number objects anyway to assign to x at some point, so you might as
well get that done with upfront.

The problem was memory consumption. The memory requirements of the
list put a bound on large your iteration could be.

xrange was created, and soon followed general iters. Now, the range
object generates the numbers on the fly as the loop runs .. the loop
says "hey, whats next" and the next item is returned.

When people say "don't say for x in range" they are really saying "use
xrange instead of range."

Apr 5 '06 #11
AndyL <ask@me> wrote:
Paul Rubin wrote:
Normally you'd use range or xrange. range builds a complete list in
memory so can be expensive if the number is large. xrange just counts
up to that number.

so when range would be used instead of xrange. if xrange is more
efficient, why range was not reimplemented?


If you actually want the list for some reason:

$ python2.4 -mtimeit 'list(xrange(100))'
100000 loops, best of 3: 4.54 usec per loop
$ python2.4 -mtimeit 'range(100)'
100000 loops, best of 3: 2.61 usec per loop

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Apr 5 '06 #12
On Wed, 05 Apr 2006 09:16:37 -0400, AndyL wrote:
Paul Rubin wrote:
Normally you'd use range or xrange. range builds a complete list in
memory so can be expensive if the number is large. xrange just counts
up to that number.


so when range would be used instead of xrange. if xrange is more
efficient, why range was not reimplemented?


For historical reasons.

Don't worry, in Python3000, range() will be an iterator, and xrange() will
disappear. Until then, I use range() for small loops (by small I mean
anything up to a few tens of thousands), and xrange() only when I
absolutely have to optimize my code to save a piddling few tens of
kilobytes of memory.
--
Steven.

Apr 5 '06 #13
On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote:
su**************@gmail.com wrote:
hi John,
Python doesn't provide for loop like C / C++ but using Range() or
Xrange() you can achive all the functionalities of the C for loop.


Not quite.


Care to explain what the differences are, or shall we guess?

--
Steven.

Apr 5 '06 #14
On Wed, 05 Apr 2006 16:21:02 +0200, Georg Brandl wrote:
Because of backwards compatibility. range() returns a list, xrange() an
iterator: list(xrange(...)) will give the same results as range(...).


Georg is pretty much correct in his explanation, but just to dot all the
I's and cross all the T's, we should explain that xrange() doesn't return
an iterator, it returns a special xrange object:
x = xrange(1000)
type(x) <type 'xrange'>

xrange existed as a special bit of magic before Python supported
iterators. While xrange objects behave (sort of) like iterators, they
aren't quite the same. For instance, they don't have a next attribute:
x.next Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'xrange' object has no attribute 'next'

whereas iterators do:
i = iter(range(1000))
i.next <method-wrapper object at 0xf7054d2c>

Likewise, you can get random access to the items in an xrange object:
x[500] 500

but not in iterators:
i[500]

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object

--
Steven.

Apr 5 '06 #15
Steven D'Aprano wrote:
On Wed, 05 Apr 2006 16:21:02 +0200, Georg Brandl wrote:
Because of backwards compatibility. range() returns a list, xrange() an
iterator: list(xrange(...)) will give the same results as range(...).


Georg is pretty much correct in his explanation, but just to dot all the
I's and cross all the T's, we should explain that xrange() doesn't return
an iterator, it returns a special xrange object:


Ah yes, the old iterator <-> iterable problem ;)

Georg
Apr 5 '06 #16
Steven D'Aprano wrote:
On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote:
su**************@gmail.com wrote:
hi John,
Python doesn't provide for loop like C / C++ but using Range() or
Xrange() you can achive all the functionalities of the C for loop.


Not quite.


Care to explain what the differences are, or shall we guess?


C's for is much more powerful.

for(a; b; c) { d } translates to

a
while b
d
c

which can't be replaced by a simple

for i in range(...)

Georg
Apr 5 '06 #17
Georg Brandl <g.*************@gmx.net> wrote:
Steven D'Aprano wrote:
On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote:
su**************@gmail.com wrote:
hi John,
Python doesn't provide for loop like C / C++ but using Range() or
Xrange() you can achive all the functionalities of the C for loop.

Not quite.


Care to explain what the differences are, or shall we guess?


C's for is much more powerful.

for(a; b; c) { d } translates to

a
while b
d
c

which can't be replaced by a simple

for i in range(...)


No, but it can be easily replaced by:

# factoring out the loopstructure...
def gotcha():
a
while b:
yield c

# ...from the loopbody
for i in gotcha():
d

or several variations thereof. In C++, I almost half-heartedly replace
most of Python's generators' power with C++'s templated iterators, but
when I program in C I find myself really straightjacketed these days in
NOT being able to pull out the "loopstructure" as I can in Python (and,
about halfway, in C++).
Alex
Apr 6 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jim Cser | last post by:
Hello- I have a function to generate a multi-dimensional array, which then gets summed over one axis. The problem is that the dimensions are large, and I run out of memory when I create the...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
29
by: Steve R. Hastings | last post by:
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list and...
45
by: Summercoolness | last post by:
it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0
1
by: shilpasharma | last post by:
Hi, Can anybody let me know how I can optimise following Query. Select * from reports where ( exists ( SELECT 1 FROM results_required rr, item_claims_trials ict, results res WHERE...
2
by: Joe Goldthwaite | last post by:
I've been playing with Python a bit. Doing little performance benchmarks and working with Psyco. It's been fun and I've been learning a lot. For example, in a previous post, I was looking for a...
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.