Definition
The filter() function returns an iterator were the items are filtered through a function to test if the item is accepted or not.
Syntax
filter(function, iterable)
Parameters
Parameter | Description |
---|---|
function | A Function to be run for each item in the iterable |
iterable | The iterable to be filtered |
Examples
myNumbers = [1, 3, 17, 18, 24, 32]
def myFunction(x):
if x == 24:
return True
else:
return False
results = filter(myFunction, myNumbers)
for x in results:
print(x)