'Advanced' list comprehension? query

Peter Otten __peter__ at web.de
Wed Aug 8 12:18:22 EDT 2007


 pyscottishguy at hotmail.com wrote:

> 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'.

>>> no_show_list = ["one", "two", "three"]
>>> my_list = ["item one", "item four", "three item"]
>>> [item for item in my_list if not any(x in item for x in no_show_list)]
['item four']

Peter



More information about the Python-list mailing list