For Counter Variable

Tim Chase python.list at tim.thechases.com
Sun Sep 23 19:29:29 EDT 2012


On 09/23/12 17:54, Steven D'Aprano wrote:
> On Sun, 23 Sep 2012 10:45:53 -0700, jimbo1qaz wrote:
>> On Sunday, September 23, 2012 9:36:19 AM UTC-7, jimbo1qaz wrote:
>>> Am I missing something obvious, or do I have to manually put in a
>>> counter in the for loops? That's a very basic request, but I couldn't
>>> find anything in the documentation.
>>
>> Ya, they should really give a better way, but for now, enumerate works
>> pretty well.
> 
> Define "a better way". What did you have in mind that would work better?

I can only imagine jimbo1qaz intended "a more C-like way".  blech.

I **far** prefer The Python Way™.  The vast majority of the time,
I'm looping over some iterable where indices would only get in the
way of readability.  Tuple-unpacking the results of enumerate() is
an elegant way of getting both the items+indices on the seldom
occasion I need the index too (though I'm minorly miffed that
enumerate()'s starting-offset wasn't back-ported into earlier 2.x
versions and have had to code around it for 1-based indexing; either
extra "+1"s or whip up my own simple enumerate() generator).

-tkc





More information about the Python-list mailing list