The filter() function takes in two arguments: a function and an iterable. It filters the elements of the iterable by calling the given function with each of the iterable's element. It returns an iterator of only those elements that evaluate to True.

Syntax:
filter(function, iterable)

Where, function is a function to be used as a condition, it should return either True or False. The iterable represents the iterable which should be filtered. Both arguments are required.

It returns an iterator object containing the elements that satisfies the given condition.

ExampleEdit & Run
num_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
def is_even(x):
    if x % 2 == 0:
        return True
    else: return False

filtered=filter(is_even, num_list)
print(filtered)
print(list(filtered))
Output:
<filter object at 0x7fbc73a3bcd0>[0, 2, 4, 6, 8][Finished in 0.010547118028625846s]

Lambda functions are often used with filter functions as the function argument, as they allow for concise filter expressions that would typically otherwise take multiple lines of code to express.

Extract odd numbers from a list of numbers: 

ExampleEdit & Run
is_odd = lambda x: x % 2 ==1
print(list(filter( is_odd, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))

greater_than_5 = lambda x: x>5
print(list(filter(greater_than_5, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])))
Output:
[1, 3, 5, 7, 9][6, 7, 8, 9, 10][Finished in 0.010909341042861342s]
ExampleEdit & Run
languages = ('PYTHON', 'css', 'JAVA', 'C++', 'html', 'RUBY', 'javascript', 'KOTLIN')
print(list(filter(lambda x: x.isupper(), languages))) #Extract words whose characters are all uppercase
Output:
['PYTHON', 'JAVA', 'C++', 'RUBY', 'KOTLIN'][Finished in 0.010174860013648868s]