[Tutor] List index usage: is there a more pythonesque way?

Alan Gauld alan.gauld at btinternet.com
Mon Apr 19 01:09:04 CEST 2010


"C M Caine" <cmcaine at googlemail.com> wrote 


>        for i in range(len(timetable)):
>            numDict[timetable[i]] += 1
>            if spaceDict['1st'+timetable[i]] == 0:
>                spaceDict['nth'+timetable[i]] = i

for index, item in enumerate(timetable):
            numDict[item] += 1
            if spaceDict['1st'+item] == 0:
                spaceDict['nth'+item] = i
     
> This function works fine, but I think that using range(len(timetable))
> is clumsy. On the other hand, I need the indexes to track the number

enumerate() is your friend.


HTH,

Alan G



More information about the Tutor mailing list