language-x-isms
Fredrik Lundh
fredrik at pythonware.com
Thu Jun 8 05:48:24 EDT 2006
Alan Kennedy wrote:
> On jython 2.1, I use something like this
>
> #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> try:
> enumerate
> except NameError:
> def enumerate(iterable):
> results = [] ; ix = 0
> for item in iterable:
> results.append( (ix, item) )
> ix = ix+1
> return results
at least in CPython, using a user-defined enumerate function is a bit
slower than using the built-in version.
in fact, if performance is important, the following can sometimes be the
most efficient way to loop over things:
ix = 0
for fibo in my_list:
do something with ix and my_list[ix]
ix += 1
</F>
More information about the Python-list
mailing list