getting an index in a for loop

Andrew Bennetts andrew-pythonlist at puzzling.org
Sat Feb 1 08:26:29 CET 2003


On Fri, Jan 31, 2003 at 07:41:38PM -0800, Erik Max Francis wrote:
> Chad Netzer wrote:
> 
> > Or the always-in-style, oldie-but-goodie method (from Python 1.x
> > onwards):
> > 
> > s = 'abcdefg'
> > for i in xrange( len( s ) ):
> >     print i, s[i]
> 
> Note this solution also does not suffer from performance problem if the
> sequence is truly large.

I believe enumerate is like xrange, in that it lazily evaluates its results,
so it should also give good performance.

-Andrew.






More information about the Python-list mailing list