search for pattern in StringList

Steven D. Majewski sdm7g at virginia.edu
Mon Dec 18 14:44:36 EST 2000


Except it should be: filter( function, sequence )

>>> print filter( lambda x: 'a' in x, MyList )
['an', 'call']


-- Steve Majewski <sdm7g @ Virginia.EDU> 


On 18 Dec 2000, Remco Gerlich wrote:

>  
> This is a very common thing to do, select some items from a list based
> on some criterium. filter() is a built-in function that does this:
> 
> print filter(MyList, lambda x: 'a' in x)
> 
> It "filters" the list according to the simple function given by the lambda,
> that tests if 'a' in x.
> 
> Since Python 2.0, there is yet another way to write this:
> 
> print [s for s in Mylist if 'a' in s]
> 
> This is derived from set notation in math; it is 'the list of s, where
> s comes from MyList and only those where "a" in s'.
> 





More information about the Python-list mailing list