please help shrink this each_with_index() implementation

akean akean at clear.net.nz
Tue Jan 5 21:17:11 CET 2010


On Jan 6, 8:58 am, Phlip <phlip2... at gmail.com> wrote:
> Hypo Nt:
>
> def each_with_index(seq):
>     index = 0
>     result = []
>
>     for item in seq:
>       result.append([item, index])
>       index += 1
>
>     return result
>
> My Pythonic sequencing skills are obviously feeble. Can anything think
> of a way to write that in fewer lines?
>
> --
>   Phlip
>  http://c2.com/cgi/wiki?MoreliaViridis

 y = [[seq[i],i] for i in range(len(seq))]  gives the same result.
The index is accessible without assigning it to another variable.
--
Anita



More information about the Python-list mailing list