remove a list from a list

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

  1. Hi,

    I have the following problem:

    I have a list like
    e = ['a', 'b', 'e']
    and another list like
    l = ['A', 'a', 'c', 'D', 'E']
    I would like to remove from l all the elements that appear in e
    case-insensitive. That is, the result would be
    r = ['c', 'D']

    What is a *nice* way of doing it?

    Thanks a lot,
    Ray
     
    Rares Vernica, Nov 17, 2006
    #1
    1. Advertising

  2. Rares Vernica wrote:
    > Hi,
    >
    > I have the following problem:
    >
    > I have a list like
    > e = ['a', 'b', 'e']
    > and another list like
    > l = ['A', 'a', 'c', 'D', 'E']
    > I would like to remove from l all the elements that appear in e
    > case-insensitive. That is, the result would be
    > r = ['c', 'D']
    >
    > What is a *nice* way of doing it?
    >
    > Thanks a lot,
    > Ray
    >


    if you can guarantee that e will always be made up of lowercase letters,
    then you can do the following:

    r = [ x for x in l if x.lower() not in e ]

    Cheers,
    Cliff
     
    J. Clifford Dyer, Nov 20, 2006
    #2
    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,413
    Henrik
    Aug 10, 2005
  2. tshad
    Replies:
    6
    Views:
    21,537
    tshad
    Aug 8, 2006
  3. Rares Vernica

    Re: remove a list from a list

    Rares Vernica, Nov 17, 2006, in forum: Python
    Replies:
    4
    Views:
    317
    John Henry
    Nov 17, 2006
  4. Rares Vernica

    Re: remove a list from a list

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

Share This Page