Alex Rivera | Logout

What is the proper way to track indexes in python?

Asked 2011-08-29T17:29:02.177
13

Right now I am tracking my index in side the loop like this

index = 0
for entry in longList:
    if entry == 'foo':
        print index
    index += 1

is there a better way to do this?

Edit
Report

1 Answer

6

I like list comprehension :)

[index for (index,entry) in enumerate(longList) if entry == 'foo']
answered 2011-08-29T17:51:01.950

Your Answer