is this pythonic?

pruebauno at latinmail.com pruebauno at latinmail.com
Thu Jan 22 10:04:38 EST 2009


On Jan 21, 4:23 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> prueba... at latinmail.com wrote:
> > ... If you have duplicates this will not work. You will have to do
> > something like this instead:
>
> >>>> o=[]
> >>>> i=0
> >>>> ln=len(l)
> >>>> while i<ln:
> >    if l[i]['title']=='ti':
> >            o.append(l.pop(i))
> >            ln-=1
> >    else:
> >            i+=1
>
> Or the following:
>      indices = [i for i,d in enumerate(l) if d['title']=='ti']
>      for i in reversed(indices): # so del doesn't affect later positions
>          del l[i]
>
> --Scott David Daniels
> Scott.Dani... at Acm.Org

Cool. How come I didn't think of that! That means I can create an evil
one liner now >:-).

replacecount=len([o.append(l.pop(i)) for i in reversed(xrange(len(l)))
if l[i]['title']=='ti'])



More information about the Python-list mailing list