Get number of iteration

F

Florian Lindner

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
 
D

Diez B. Roggisch

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
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
 
J

Jeff Epler

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
 
P

Peter Otten

Florian said:
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"
.... print index, item
....
0 tree
1 for
2 five

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

Peter
 
P

Paul Prescod

Diez said:
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
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
 
S

Sidharth Kuruvila

there is a function in python 2.3 called enumerate.

[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)].... 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[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)]
 
S

Sidharth Kuruvila

there is a function in python 2.3 called enumerate.

[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)].... 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[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)]
 
?

=?ISO-8859-1?Q?J=F8rgen_Cederberg?=

Jeff said:
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
 
P

Paul Rubin

Jørgen Cederberg said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top