remove a list from a list

John Henry john106henry at hotmail.com
Fri Nov 17 20:48:36 CET 2006


Scratch that.  b becomes all upper...

John Henry wrote:
> from sets import Set as set   # Python 2.3
>
> b = list( set([i.upper() for i in b) - set([i.upper() for i in a] ) )
>
>
> Rares Vernica wrote:
> > Yeah, I ended up doing a similar kind of loop. That is pretty messy.
> >
> > Is there any other way?
> >
> > Thanks,
> > Ray
> >
> > Tim Chase wrote:
> > >> That is a nice solution.
> > >>
> > >> But, how about modifying the list in place?
> > >>
> > >> That is, l would become ['c', 'D'].
> > >>
> > >>>  >>> e = ['a', 'b', 'e']
> > >>>  >>> l = ['A', 'a', 'c', 'D', 'E']
> > >>>  >>> s = set(e)
> > >>>  >>> [x for x in l if x.lower() not in s]
> > >>> ['c', 'D']
> > >
> > >
> > > Well...changing the requirements midstream, eh? ;-)
> > >
> > > You can just change that last item to be a reassignment if "l" is
> > > all you care about:
> > >
> > >  >>> l = [x for x in l ...]
> > >
> > > Things get a bit hairier if you *must* do it in-place.  You'd
> > > have to do something like this (untested)
> > >
> > > for i in xrange(len(l), 0, -1):
> > > 	if l[i-1].lower() in s:
> > > 		del l[i-1]
> > >
> > >
> > > which should do the job.
> > > 
> > > -tkc
> > > 
> > > 
> > >




More information about the Python-list mailing list