Filter() method in Python Built in Function
Filter method in built in function removes the objects which are not required and keeps only the desired objects.
Syntax:
filter(function, Iterable)
Parameter
filter method has two parameters.
- Function:this tests if the elements in the Iterable are true or false. If it si neither true nor false i.e. if it is none then the function defaults to identify the function and this returns false if any elements are false.
- Iterable:these are the objects which needs to be filtered it could be tuple, sets or lists or any other objects which contains iterators.
Example-1:
age = [5, 12, 17, 18, 24, 32]
defmyFunc(x):
if x < 18:
return False
else:
return True
adults = filter(myFunc, age)
for x in adults:
print(x)
Output:

Example-2:
x = [10, 'ca', False, True, '01']
filteredList = filter(None, x)
print('Elements in x are:')
for element in filteredList:
print(element)x = 'a = 8\nb=10\nprint("add of =", a+b)'
exec(x)
Output: