Re: remove a list from a list

Discussion in 'Python' started by Rares Vernica, Nov 17, 2006.

  1. 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
    >
    >
    >
     
    Rares Vernica, Nov 17, 2006
    #1
    1. Advertising

  2. Rares Vernica

    John Henry Guest

    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
    > >
    > >
    > >
     
    John Henry, Nov 17, 2006
    #2
    1. Advertising

  3. Rares Vernica

    Tim Chase Guest

    > 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] ) )


    Just a caveat...this can change the order of items in the results
    as sets (and their differences) are inherently unordered data
    structures. If order of the items in the list need to preserved,
    it must remain as a list. However, if the order of the resulting
    list doesn't matter, this is a good solution.

    -tkc
     
    Tim Chase, Nov 17, 2006
    #3
  4. Rares Vernica

    John Henry Guest

    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
    > > >
    > > >
    > > >
     
    John Henry, Nov 17, 2006
    #4
  5. Rares Vernica

    John Henry Guest

    OK, if you don't care the resulting order, do it like:

    class Convert2Dict:
    def __init__(self, data):
    self._data={}
    for x in data:
    self._data[x.upper()]=x
    def get(self, key):
    return self._data[key.upper()]

    a = ["a", "B"]
    b = ["c", "a", "A", "D", "b"]
    b_dict = Convert2Dict(b)

    b = [b_dict.get(x) for x in list( set([x.upper() for x in b]) -
    set([x.upper() for x in a]) ) ]


    John Henry wrote:
    > 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
    > > > >
    > > > >
    > > > >
     
    John Henry, Nov 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,373
    Henrik
    Aug 10, 2005
  2. tshad
    Replies:
    6
    Views:
    21,469
    tshad
    Aug 8, 2006
  3. Rares Vernica

    remove a list from a list

    Rares Vernica, Nov 17, 2006, in forum: Python
    Replies:
    1
    Views:
    256
    J. Clifford Dyer
    Nov 20, 2006
  4. Rares Vernica

    Re: remove a list from a list

    Rares Vernica, Nov 17, 2006, in forum: Python
    Replies:
    5
    Views:
    291
    Rares Vernica
    Nov 17, 2006
  5. Andy
    Replies:
    3
    Views:
    487
    James Kanze
    Jun 8, 2007
Loading...

Share This Page