473,654 Members | 3,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1467
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.repea t(). 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
3946
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 (1064263264 => "event1", 10642635555 => "event2", 1064266666 => "event3", 1064267782 => "event4", 1064268812 => "event5"); I basically want to do a "for" or "foreach" but I don't necessarily want to start at key 1064263264 (event1).
0
2121
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
1634
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
1297
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 GroupMembers. As i iterate, i'm adding to the Tag property either the instance of that specific Group or GroupMember. Works fine, all is good. My question is this....I need to know if the user selected on the Tree a Group "node" or a GroupMember...
2
12626
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 possible values out so I can check each value Cheers
5
29346
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 iterate through them?
3
2019
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 to strip out all the HTML leaving only the raw text. (done and works a treat) My issue is:
9
7877
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 List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
25
25437
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 example list: function myFunction() { createDOM({ 'id' : , 'dom' : , 'parent' : "content", 'form' : ,
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.