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

enumerate vs while

Sorry for the simplicity of my questions, but is enumerate a way around using while loops

my while loop is this:
Expand|Select|Wrap|Line Numbers
  1. x = 1
  2. >>> while x < len(rate):
  3. ...     try:
  4. ...         growth.append(rate[x])
  5. ...         x += 3
  6. ...     except IndexError:
  7. ...         break
  8. ... 
thanks
Sep 27 '07 #1
3 1762
bvdet
2,851 Expert Mod 2GB
Sorry for the simplicity of my questions, but is enumerate a way around using while loops

my while loop is this:
Expand|Select|Wrap|Line Numbers
  1. x = 1
  2. >>> while x < len(rate):
  3. ...     try:
  4. ...         growth.append(rate[x])
  5. ...         x += 3
  6. ...     except IndexError:
  7. ...         break
  8. ... 
thanks
Use enumerate() when you need an index number and an item from an iterable:
Expand|Select|Wrap|Line Numbers
  1. for i, item in enumerate(rates):
  2.     if not (i-1) % 3:
  3.         growth.append(item)
Another form using range():
Expand|Select|Wrap|Line Numbers
  1. for i in range(1, len(rates), 3):
  2.     growth.append(rates[i])
Sep 28 '07 #2
ghostdog74
511 Expert 256MB
Sorry for the simplicity of my questions, but is enumerate a way around using while loops

my while loop is this:
Expand|Select|Wrap|Line Numbers
  1. x = 1
  2. >>> while x < len(rate):
  3. ...     try:
  4. ...         growth.append(rate[x])
  5. ...         x += 3
  6. ...     except IndexError:
  7. ...         break
  8. ... 
thanks
a better way to create a while loop (IMO) is using infinity loop.
eg
Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     #statements...
  3.     if some condition:
  4.         break
  5.     if another condition:
  6.         break
  7.  
this way, you can customize your loop according to different conditions, whereas in your example, you can only exit while loop on x>len(rate).
and as to your question, enumerate() works on iterable objects, ie range(), open files, etc.. see here.
Sep 28 '07 #3
bartonc
6,596 Expert 4TB
a better way to create a while loop (IMO) is using infinity loop.
eg
Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     #statements...
  3.     if some condition:
  4.         break
  5.     if another condition:
  6.         break
  7.  
this way, you can customize your loop according to different conditions, whereas in your example, you can only exit while loop on x>len(rate).
<snip>
I also like the infinite loop implementation and use enumerate() often. That said: Lest our readers get the wrong impression:
Expand|Select|Wrap|Line Numbers
  1. while x < len(rate):
  2.     #statements...
  3.     if some condition:
  4.         break
  5.     if another condition:
  6.         break
  7.  
is also valid.
Sep 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Pekka Niiranen | last post by:
Hi, I have Perl code looping thru lines in the file: line: while (<INFILE>) { ... $_ = do something ... if (/#START/) { # Start inner loop
2
by: Chris Hohmann | last post by:
Does anyone know of a way to enumerate the local variable scope? The reason I ask is that I have a function that dumps the contents of response,request,server,etc... for debugging purposes. I'd...
5
by: Jeff Grundy | last post by:
How do I enumerate the machines domain, then enumerate the shares of a machine?
3
by: Cliff Harris | last post by:
I have an odd question that I'm hoping someone can help with. I simply (or not simply?) need to enumerate through all of the data types in the .Net framework. I do understand that if this is...
1
by: smichr | last post by:
I see that there is a thread of a similar topic that was posted recently ( enumerate with a start index ) but thought I would start a new thread since what I am suggesting is a little different. ...
6
by: Gregory Petrosyan | last post by:
Hello! I have a question for the developer of enumerate(). Consider the following code: for x,y in coords(dots): print x, y When I want to iterate over enumerated sequence I expect this to...
2
by: eight02645999 | last post by:
hi, i am using python 2.1. Can i use the code below to simulate the enumerate() function in 2.3? If not, how to simulate in 2.1? thanks from __future__ import generators def...
21
by: James Stroud | last post by:
I think that it would be handy for enumerate to behave as such: def enumerate(itrbl, start=0, step=1): i = start for it in itrbl: yield (i, it) i += step This allows much more flexibility...
11
by: crwe | last post by:
Hello all, in python2.4, i read lines from a file with for lineNum, line in enumerate(f): ... However, lineNum soon overflows and starts counting backwards. How do i force enumerate to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.