473,320 Members | 1,535 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,320 software developers and data experts.

how best to iterate i times ?

In the case I know how may times I want to iterate, one way to do it is
like this:

for i in range(100000):
dothis()

I like how clean this syntax is, but this method is very wasteful. The
list created by range(100000) consumes a fair amount of memory and it is
only used to iterate.

This uses less memory:

i = 0
while i <= 100000:
dothis()
i = i + 1
del i

The problem with the while loop is that it is ugly. I have to introduce
a new variable i, and assign a new value every time I loop. Also, any
links I may have to i, say (y = i) will break when I reassign with (i =
i + 1) rather than y continuing to = i.

Example
i = 0
y = i
while i <= 100000:
dothis()
i = i + 1

print i # i == 100001
print y # y == 0

There are times when I need to know the state of an 'iterator'.

So, question is: What is the efficient, elegant, pythonic way to
iterate with integers?

Randall
Jul 18 '05 #1
4 1443
On Wed, 17 Mar 2004 19:07:36 GMT, Randall Smith <ra*****@tnr.cc> wrote:
In the case I know how may times I want to iterate, one way to do it is
like this:

for i in range(100000):
dothis()


I believe that xrange does the same without using much memory.
--
http://www.homepages.lu/pu/
Jul 18 '05 #2
Randall Smith wrote:
So, question is: What is the efficient, elegant, pythonic way to
iterate with integers?


The standard way is to use xrange() instead of range(). If you are
interested only in the repetition, not the numbers, you can also use
itertools.repeat(). Let's see what is fastest:

(1)
for i in xrange(N):
pass

(2)
for _ in repeat(None, N):
pass

$ timeit.py "for i in xrange(100000): pass"
100 loops, best of 3: 1.09e+04 usec per loop
$ timeit.py -s"from itertools import repeat" "for i in repeat(None, 100000):
pass"
100 loops, best of 3: 5.73e+03 usec per loop
Peter
Jul 18 '05 #3
Randall Smith wrote:
for i in range(100000):
dothis()

I like how clean this syntax is, but this method is very wasteful.
The list created by range(100000) consumes a fair amount of memory
and it is only used to iterate. .... So, question is: What is the efficient, elegant, pythonic way to
iterate with integers?


Solely for that purpose 'xrange' was created:

for i in xrange(100000):
dothis()

HTH,
Mike


Jul 18 '05 #4
Thank you all.

You were all very helpful.
Randall

Mike Rovner wrote:
Randall Smith wrote:
for i in range(100000):
dothis()

I like how clean this syntax is, but this method is very wasteful.
The list created by range(100000) consumes a fair amount of memory
and it is only used to iterate.


...
So, question is: What is the efficient, elegant, pythonic way to
iterate with integers?

Solely for that purpose 'xrange' was created:

for i in xrange(100000):
dothis()

HTH,
Mike


Jul 18 '05 #5

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

Similar topics

6
by: ChronoFish | last post by:
Hi there, I want to iterate through an array starting at a known index. However the indexes are not linear. For example I have an array of events keyed by timestamp. $eventList = array...
0
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
3
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
7
by: Doug Handler | last post by:
Hi, I'm using a modified Tree control that contains a Tag property of Object. I iterate through two different tables to build the Tree w/ root nodes being Groups and child nodes being...
2
by: Tommo | last post by:
Chaps, Could someone please provide me some examples of iterating through a multimap. I wish to do the following 1. Iterate through each key in the multimap 2. For each key i find get all the...
5
by: jaso | last post by:
Hi, If have a structure of a database record like this: struct record { char id; char title; ... }; Is there some way to find out how many member variables there is in the struct and then...
3
by: Russell | last post by:
Hey, ok i have numerous tables to search through for a 'site search'. some of the searchble fields have html embeded within so after some quick referencing, saw I can use the regExp function...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
25
RMWChaos
by: RMWChaos | last post by:
Any JSON experts out there? I'd like to know if it is possible, and if so how, to iterate through a JSON property list so that each iteration selects the next value for each object. Here is an...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.