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

Get number of iteration

Hi!
If I iterate through a list, is there a way I can get the number of the
iteration: first, second, third, ...

l = ["three", "four", "five", "six"]
for x in l
print x
print x.iteration() # <- That's what I'm looking for!
print "next"

prints that:

three
1
next
four
2
next
fixe
3
next
six
4
next

Thx,
Florian
Jul 18 '05 #1
10 2736
> If I iterate through a list, is there a way I can get the number of the
iteration: first, second, third, ...

l = ["three", "four", "five", "six"]
for x in l
print x
print x.iteration() # <- That's what I'm looking for!
print "next"


No, this won't work - x is the value of the list element, not an c++-like
iterator-object (that has to be dereferenced before accessing the actual
value).

So usually x won't have an iteration-method. But of course you can alwas do
this:

for i in xrange(len(l)):
x = l[i]
print x
print i
print "next"

Or - if len() can't be applied to your sequence for whatever reason, as it
is e.g. a iterable object, you can of course keep track with your own
counter:

i = 0
for x in some_iterable_thingy:
print x
print i
i += 1
print "next"

Regards,

Diez
Jul 18 '05 #2
Florian Lindner wrote:
If I iterate through a list, is there a way I can get the number of the
iteration: first, second, third, ...

l = ["three", "four", "five", "six"]
for x in l
print x
print x.iteration() # <- That's what I'm looking for!
print "next"

sample = "tree for five".split()
for index, item in enumerate(sample):

.... print index, item
....
0 tree
1 for
2 five

You are looking for enumerate(). Counting starts at 0, though.

Peter
Jul 18 '05 #3
there is a function in python 2.3 called enumerate.

l = range(0,51,5)
e = enumerate(range(0,51,5))
e <enumerate object at 0x0119BBC0> list(e) [(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)] e = enumerate(range(0,51,5))
for index, value in e: .... print "The index of value %i is %i" % (value, index)
....
The index of value 0 is 0
The index of value 5 is 1
The index of value 10 is 2
..
..
..

if you are using an older version of python which doesn't have enumerate
you could do this l = range(0,51,5)
zip(range(len(l)), l)

[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)]

Jul 18 '05 #4
there is a function in python 2.3 called enumerate.

l = range(0,51,5)
e = enumerate(range(0,51,5))
e <enumerate object at 0x0119BBC0> list(e) [(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)] e = enumerate(range(0,51,5))
for index, value in e: .... print "The index of value %i is %i" % (value, index)
....
The index of value 0 is 0
The index of value 5 is 1
The index of value 10 is 2
..
..
..

if you are using an older version of python which doesn't have enumerate
you could do this l = range(0,51,5)
zip(range(len(l)), l)

[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)]

Jul 18 '05 #5

Florian> If I iterate through a list, is there a way I can get the
Florian> number of the iteration: first, second, third, ...

Sure:
for i, what in enumerate(["three", "four", "five", "six"]):

... print i, what
...
0 three
1 four
2 five
3 six

Skip

Jul 18 '05 #6
You're looking for the "enumerate" builtin function in 2.3.

for i, x in enumerate(l):
print x
print i
print "next"

You can define a "poor man's" enumerate in 2.2:

def enumerate(seq):
return zip(range(len(seq)), l)

but the enumerate in 2.3 is an iterator, not a sequence.

Jeff

Jul 18 '05 #7
Diez B. Roggisch wrote:
If I iterate through a list, is there a way I can get the number of the
iteration: first, second, third, ...

l = ["three", "four", "five", "six"]
for x in l
print x
print x.iteration() # <- That's what I'm looking for!
print "next"

...

So usually x won't have an iteration-method. But of course you can alwas do
this:

for i in xrange(len(l)):
x = l[i]
print x
print i
print "next"


In recent versions of Python there is an easier way:
for index, value in enumerate(["three", "four", "five", "six"]):

.... print index, value
....
0 three
1 four
2 five
3 six

Paul Prescod

Jul 18 '05 #8
> In recent versions of Python there is an easier way:
>>> for index, value in enumerate(["three", "four", "five", "six"]):

... print index, value
...
0 three
1 four
2 five
3 six


Boy, one never stops learning. I'll better stop answering here and read
only :)

Diez
Jul 18 '05 #9
Jeff Epler wrote:
You're looking for the "enumerate" builtin function in 2.3.

for i, x in enumerate(l):
print x
print i
print "next"

You can define a "poor man's" enumerate in 2.2:

def enumerate(seq):
return zip(range(len(seq)), l)


Just nitpicking... :), but I guess you ment:

def enumerate(seq):
return zip(range(len(seq)), seq)

/Jorgen
Jul 18 '05 #10
Jørgen Cederberg <jo*************@hotmail.com> writes:
Just nitpicking... :), but I guess you ment:

def enumerate(seq):
return zip(range(len(seq)), seq)


I think you want:

def enumerate(seq):
for i in xrange(len(seq)):
yield (i, seq[i])
Jul 18 '05 #11

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

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
5
by: Florian Lindner | last post by:
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
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
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...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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,...

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.