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

C M Caine cmcaine at googlemail.com
Mon Apr 19 01:37:11 CEST 2010


That's two new things I've learnt. I didn't realise that for loops
could be used like that (with more than one... key?).

Thanks, I'm changing my code even now!

On 19 April 2010 00:09, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "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
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list