Simple python iteration question

programmer.py at gmail.com programmer.py at gmail.com
Tue Aug 14 12:37:53 EDT 2007


On Aug 14, 11:27 am, ra... at dot.com (Lawrence Oluyede) wrote:
> Bryan <b... at bcc.com> wrote:
> > How do I do this?
>
> for i, item in enumerate(l):
>     print i, item
>

^^ That is the `most-correct` answer.  But because you're new, I'll
take this time to introduce you to help(), just in case you're too
lazy to RTFM (like me ;)).  help works on objects, and things you may
not expect  - try help(2).  Oh and welcome to `fun` programming!

>>> help(enumerate)

class enumerate(object)
 |  enumerate(iterable) -> iterator for index, value of iterable
 |
 |  Return an enumerate object.  iterable must be an other object that
supports
 |  iteration.  The enumerate object yields pairs containing a count
(from
 |  zero) and a value yielded by the iterable argument.  enumerate is
useful
 |  for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2,
seq[2]), ...

.. and a bunch of stuff I clipped ...

hth,
jw




More information about the Python-list mailing list