simple question about Python list

Arnaud Delobelle arnodel at googlemail.com
Fri May 9 07:13:46 EDT 2008



dmitrey wrote:
> Ok, I use Python 2.5 but I try my code to remain Python 2.4 and
> (preferable) 2.3 compatible.
> Are there other solutions?
> D.
>
> On 9 ���, 13:17, Paul Hankin <paul.han... at gmail.com> wrote:
> > On May 9, 11:04�am, dmitrey <dmitrey.kros... at scipy.org> wrote:
> >
> > > list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
> > > and
> > > list2 = [0, 2, 4] # integer elements
> >
> > > howto (I mean most simple recipe, of course) form list3 that contains
> > > elements from list1 with indexes from list2, i.e.
> >
> > list3 = [list1[i] for i in list2]
> >
> > --
> > Paul Hankin

This works under python 2.0+, I think.

Before that, you could have written:

list3 = map(list1.__getitem__, list2)

--
Arnaud



More information about the Python-list mailing list