indexed() generator

Magnus Lie Hetland mlh at vier.idi.ntnu.no
Sat Jan 26 14:38:51 EST 2002


In article <a2rt58$gpr$1 at serv1.iunet.it>, Alex Martelli wrote:
>"Michael Chermside" <mcherm at destiny.com> wrote in message
>news:mailman.1011968940.24241.python-list at python.org...
>    ...
>>   * The following approach is simpler and cleaner _IF_ the reader is
>>     familiar with the convention:
>>
>>      # Approach 3:
>>      for i, item in indexed(list):
>>          <code with i and item>
>    ...
>> So... anyone else agree?
>
>Yes.  See also:
>http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52233
>
[...]
>or of course, as multiply suggested in this thread,
>
>
>from __future__ import generators
>
>def indexed(seq):
>    seq = iter(seq)

Why do you need this step? (Just curious)

>    i = 0
>    for item in seq:
>        yield i, item
>        i += 1
>
>
>Alex

--
Magnus Lie Hetland                                  The Anygui Project
http://hetland.org                                  http://anygui.org



More information about the Python-list mailing list