473,403 Members | 2,222 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,403 software developers and data experts.

Getting number of iteration

Hello,
when I'm iterating through a list with:

for x in list:

how can I get the number of the current iteration?

Thx,

Florian
Jul 19 '05 #1
5 5205
On 5/6/05, Florian Lindner <Fl*************@xgm.de> wrote:
Hello,
when I'm iterating through a list with:

for x in list:

how can I get the number of the current iteration?


Python 2.4 and greater:

for n, x in enumerate(lst):
print "iteration %d on element %s" % (n, x)

Earlier:

n = 0
for x in lst:
print "iteration %d on element %s" % (n, x)
n += 1

And you shouldn't use list as a variable name; list() is a built-in
function which you'll clobber if you do.

Peace
Bill Mill
bill.mill at gmail.com
Jul 19 '05 #2
On 5/6/05, Bill Mill <bi*******@gmail.com> wrote:
On 5/6/05, Florian Lindner <Fl*************@xgm.de> wrote:
Hello,
when I'm iterating through a list with:

for x in list:

how can I get the number of the current iteration?
Python 2.4 and greater:


ummm, make that 2.3 and greater. I always think things are more recent
than they are.

for n, x in enumerate(lst):
print "iteration %d on element %s" % (n, x)

Earlier:

n = 0
for x in lst:
print "iteration %d on element %s" % (n, x)
n += 1

And you shouldn't use list as a variable name; list() is a built-in
function which you'll clobber if you do.

Peace
Bill Mill
bill.mill at gmail.com

Jul 19 '05 #3
"Florian Lindner" wrote:
Hello,
when I'm iterating through a list with:

for x in list:

how can I get the number of the current iteration?

Thx,

Florianfor


in python 2.3+:

for i,x in enumerate(sequence):
print "sequence[%d] = %s" %(i,x)
George

Jul 19 '05 #4
Bill Mill <bi*******@gmail.com> writes:
On 5/6/05, Florian Lindner <Fl*************@xgm.de> wrote:
Hello,
when I'm iterating through a list with:

for x in list:

how can I get the number of the current iteration?

Earlier:

n = 0
for x in lst:
print "iteration %d on element %s" % (n, x)
n += 1


Just for the record, the old idiom was:

for n in xrange(len(lst)):
x = lst[n]
print "iteration %d on element %s" % (n, x)

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #5
Mike Meyer wrote:
n = 0
for x in lst:
print "iteration %d on element %s" % (n, x)
n += 1


Just for the record, the old idiom was:

for n in xrange(len(lst)):
x = lst[n]
print "iteration %d on element %s" % (n, x)


it was? of the following four solutions,

for n in xrange(len(lst)):
x = lst[n]
...

for n in range(len(lst)):
x = lst[n]
...

n = 0
for x in lst:
...
n += 1

for x, n in enumerate(lst):
...

the xrange solution tends to be the slowest, especially for
relatively short lists (up to a 1000 elements, or so).

the exact details vary somewhat between Python versions,
but the += solution is always a good choice, and the xrange
solution is almost always a bad choice.

</F>

Jul 19 '05 #6

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

Similar topics

17
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
10
by: Florian Lindner | last post by:
Hi! If I iterate through a list, is there a way I can get the number of the iteration: first, second, third, ... l = for x in l print x print x.iteration() # <- That's what I'm looking for!...
5
by: Paul H | last post by:
How do you folks get a reliable and complete brief of what is required before development starts? I am forever going back to a client once a project has started saying "Hang on, now that I've...
14
by: John J. Hughes II | last post by:
Using the below code I am send multiple sterilized object across an IP port. This works fine if only one object is received at a time but with packing sometimes there is more then one object or...
3
by: Rahul Anand | last post by:
As per our requirements we have a web service which internally connects (Simple HTTP Post Request) to a remote server to initiate some work. We are calling the web service method asynchronously...
4
by: jtertin | last post by:
I want to put a button on a continuous form. I would like the value of the button to represent the iteration number of the form. For example, if a form iterates (continues) for 10 records in a...
6
by: vbwire | last post by:
i use vb 6.0 Option Explicit Dim Error As Double Dim x As Integer Dim y As Double Dim z As Integer
0
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.