indexed() generator

Jason Orendorff jason at jorendorff.com
Tue Jan 22 20:28:16 EST 2002


Here's a builtin candidate for discussion.
A common Python idiom goes like this:

  for i in range(len(seq)):
      obj = seq[i]
      ...
      ...

People have complained that this is clunky.
But with this:

  from __future__ import generators

  def indexed(src):
      i = 0
      for obj in src:
          yield i, obj
          i += 1

You can write this instead:

  for i, obj in indexed(seq):
      ...
      ...

So is this good, bad, or indifferent?  (Or has it
been discussed before and I missed it?)

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list