How To Use Filter Function In Python

1. DEFINITION

2. EXAMPLE

""" FILTER WITH LAMBDA FILTERING FUNCTION """
>>> positives = filter(lambda x: x > 0, [1, -5, 0, 6])
""" RETURNS LAZY ITERABLE """
>>> positives
<filter object at 0x0250F628>
""" FORCE EVALUATION WITH A LIST CONSTRUCTOR """
>>> list(positives)
[1, 6]

3. LIKE AND UNLIKE MAP FUNCTION

4. NONE AS THE FIRST ARGUMENT

>>> trues = filter(None,[0,1,False,True,[],[1,2,3],"","hello"])
>>> list(trues)
[1, True, [1, 2, 3], 'hello']

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store