'Advanced' list comprehension? query

pyscottishguy at hotmail.com pyscottishguy at hotmail.com
Wed Aug 8 18:00:12 CEST 2007


Hi,

I'm playing around with list comprehension, and I'm trying to find the
most aesthetic way to do the following:

I have two lists:

noShowList = ['one', 'two', 'three']

myList = ['item one', 'item four', 'three item']

I want to show all the items from 'myList' that do not contain any of
the strings in 'noShowList'.

i.e. 'item four'

I can do it like this:

def inItem(noShowList, listitem):
    return [x for x in noShowList if x in listitem]

print [x for x in myList if not inItem(noShowList, x)]

and I can do it (horribly) with:

print [x for x in myList if not (lambda y, z:[i for i in y if i in z])
(noShowList, x)]

I can also print out the items that DO contain the 'noShowList'
strings with:

print [x for x in myList for y in noShowList if y in x]

but I can't get the 'not' bit to work in the above line.

Any ideas?
Thanks!




More information about the Python-list mailing list