Extracting an undefined number of items from a list

vsoler vicente.soler at gmail.com
Mon Apr 12 16:16:40 EDT 2010


On 12 abr, 22:11, Patrick Maupin <pmau... at gmail.com> wrote:
> On Apr 12, 3:05 pm, vsoler <vicente.so... at gmail.com> wrote:
>
> > Hi everyone,
>
> > say that 'db' is a list of values
> > say 'i' is a list of indexes
> > I'd like to get a list where each item is i-th element of db.
>
> > For example:
>
> > db=[10,20,30,40,50,60,70,80,90]      #undefined length
> > i=[3,5,7]                                         #undefined length
> > then result=[30,50,70]                     # resulting list
>
> > how can I do this?
>
> With a list comprehension:
>
> >>> db=[10,20,30,40,50,60,70,80,90]
> >>> i=[3,5,7]
> >>> [db[x-1] for x in i]
>
> [30, 50, 70]
>
> Regards,
> Pat

Thank you Patrick,

Your quick answer only tells me how much I still have to learn.



More information about the Python-list mailing list