This function is useful for removing some of the elements of a sequence. You must provide a filtering function that takes one argument and returns a Boolean value. Here is the calling sequence:
filter(f,S)
The filtering function is the first argument. It is applied to every
element of some sequence f. The result is a new sequence of the same type as
S, containing only
those elements S
of x for which
S returned f(x)True.
>>> def isOdd(x): ... if (x%2) == 1: return True ... else: return False ... >>> filter(isOdd, [88, 43, 65, -11, 202]) [43, 65, -11] >>> filter(isOdd, (1, 2, 4, 6, 9, 3, 3)) (1, 9, 3, 3) >>> def isLetter(c): ... return c.isalpha() ... >>> filter(isLetter, "01234abcdeFGHIJ*(&!^") 'abcdeFGHIJ'