count in 'for ... in ...'

David Bolen db3l at fitlinxx.com
Tue Nov 6 16:53:42 EST 2001


"Achim Domma" <achim.domma at syynx.de> writes:

> "Uwe Schmitt" <uwe at rocksport.de> wrote in message
> news:9s91c3$94mgs$1 at hades.rz.uni-sb.de...
> 
> > | for i in range(len(my_list)):
> > |     print i,". ",my_list[i]
> 
> > What about the following ?
> >
> >    idx=0
> >    for s in my_list:
> >        print idx,".",s
> >        idx+=1
> 
> two more lines ... not better, isn't it ?  ;-)

Well, but better need not imply shorter, unless you're in something
like the obfuscated C contest :-)

I'm not sure there's that much of a difference in this case, but the
second form is arguably more readable since the primary function
(iterating over the list) is more clearly stated, and the use of the
index variable is clear.

Then again, I've done the former myself at least as much as the latter, 
as I continue to battle my C background pulling me towards brevity :-)

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list