# can I get the index number in for x in y loop?

Discussion in 'Python' started by JuHui, Apr 3, 2006.

1. ### JuHuiGuest

>>> a='String'
>>> for x in a:

.... print x
....
S
t
r
i
n
g
>>>

can I get the index number of a in the upon loop within for x in a
loop?

JuHui, Apr 3, 2006

2. ### LuigiGuest

Try this:

>>> a='String'
>>> i=0
>>> for x in a:

.... print i, x
.... i+=1
....
0 S
1 t
2 r
3 i
4 n
5 g

Luigi, Apr 3, 2006

3. ### Paul McGuireGuest

"JuHui" <> wrote in message
news:...
> >>> a='String'
> >>> for x in a:

> ... print x
> ...
> S
> t
> r
> i
> n
> g
> >>>

>
> can I get the index number of a in the upon loop within for x in a
> loop?
>

Use enumerate. See example below.

-- Paul

>>> a = "String"
>>> for x in a:

.... print x
....
S
t
r
i
n
g
>>> for x in enumerate(a):

.... print x
....
(0, 'S')
(1, 't')
(2, 'r')
(3, 'i')
(4, 'n')
(5, 'g')
>>>

Paul McGuire, Apr 3, 2006
4. ### Rune StrandGuest

JuHui wrote:
> >>> a='String'
> >>> for x in a:

> ... print x
> ...
> S
> t
> r
> i
> n
> g
> >>>

>
> can I get the index number of a in the upon loop within for x in a
> loop?

for x, y in enumerate(a)
print x, y

Rune Strand, Apr 3, 2006
5. ### JuHuiGuest

which one has best performance?

a:for i in range(0,len(a))
b:for x in a
c:for x,y in enumerate(a)

but, it seems I cann't get index number with b format..

JuHui, Apr 3, 2006
6. ### Felipe Almeida LessaGuest

Em Seg, 2006-04-03 Ã s 08:47 -0700, JuHui escreveu:
> which one has best performance?

Let's see...

> a:for i in range(0,len(a))

\$ python2.4 -mtimeit -s 'a=[None]*100' 'for i in range(len(a)):
j = a
'
100000 loops, best of 3: 17.7 usec per loop

\$ python2.4 -mtimeit -s 'a=[None]*100' 'for i in xrange(len(a)):
j = a
'
100000 loops, best of 3: 16.8 usec per loop

> b:for x in a

\$ python2.4 -mtimeit -s 'a=[None]*100' 'i = 0
for j in a:
i += 1
'
100000 loops, best of 3: 15.7 usec per loop

> c:for x,y in enumerate(a)

\$ python2.4 -mtimeit -s 'a=[None]*100' 'for i, j in enumerate(a):
pass
'
100000 loops, best of 3: 12.9 usec per loop

Using enumerate is cleaner and faster.

HTH,

--
Felipe.

Felipe Almeida Lessa, Apr 3, 2006
7. ### JuHuiGuest

thanks a lot!

JuHui, Apr 3, 2006
8. ### Scott David DanielsGuest

JuHui wrote:
> which one has best performance?
>
> a:for i in range(0,len(a))
> b:for x in a
> c:for x,y in enumerate(a)

Read up on the timeit module and figure it out for yourself.

> but, it seems I can't get index number with b format..

Well, that's true, but it is a bit like saying:

I cannot find the distance in meters between Paris and London with:
for i in range(10):
print i

--Scott David Daniels

Scott David Daniels, Apr 3, 2006
9. ### John HunterGuest

>>>>> "Scott" == Scott David Daniels <> writes:

Scott> I cannot find the distance in meters between Paris and
Scott> London with: for i in range(10): print i

Works for me

def range(x):
yield '332.8 km'

for i in range(10):
print i

....may not be considered best practice, though <wink>

JDH

John Hunter, Apr 3, 2006
10. ### FuzzymanGuest

JuHui wrote:
> >>> a='String'
> >>> for x in a:

> ... print x
> ...
> S
> t
> r
> i
> n
> g
> >>>

>
> can I get the index number of a in the upon loop within for x in a
> loop?

Although enumerate is the 'right' answer, I personally prefer :

i = 0
while i < len(some_sequence):
val = some_sequence
...
i += 1

This is so that I can manually wind 'i' backwards and forwards manually
from other parts of the code.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Fuzzyman, Apr 3, 2006