For Counter Variable

Alec Taylor alec.taylor6 at gmail.com
Sun Sep 23 19:52:23 EDT 2012


You can always use a counter if you don't like our fancy for-each loops;

foolist = [1,24,24,234,23,423,4]
for i in xrange(len(foolist)):
    print foolist[i]

On Mon, Sep 24, 2012 at 9:29 AM, Tim Chase <python.list at tim.thechases.com>wrote:

> 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
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120924/3ade2b5b/attachment.html>


More information about the Python-list mailing list