NEWB: General purpose list iteration?

Donald Newcomb DRNewcomb at
Sat Aug 13 00:33:20 CEST 2005

"Peter Otten" <__peter__ at> wrote in message
news:ddhh60$d8s$00$1 at
> A non-recursive approach:
> def enumerate_ex(items):
>     stack = [(enumerate(items), items)]
>     while stack:
>         en, seq = stack[-1]
>         for index, item in en:
>             if isinstance(item, list):
>                 stack.append((enumerate(item), item))
>                 break
>             yield index, seq[index], seq
>         else:
>             stack.pop()

It's going to take me a while to figure out exactly what that does but it
sure does what I wanted it to.

Donald Newcomb
DRNewcomb (at) attglobal (dot) net

More information about the Python-list mailing list