Python filter() Function with Examples

In the previous article, we have discussed Python enumerate() Function with Examples
filter() Function in Python:

The filter() function returns an iterator in which the items are filtered through a function to determine whether or not the item is accepted.

Syntax:

filter(function, iterable)

Parameter Values:

function: It is a function that will be executed for each item in the iterable.

iterable: The iterable that will be filtered.

Return Value:

The iterator is returned by the filter() function.

Examples:

Example1:

Input:

Given List = [3, 10, 12, 13, 20, 7, 1, 16]

Output:

The Even numbers in a given list :
10
12
20
16

Example2:

Input:

Given List = [4, 6, 7, 9, 10, 12, 11]

Output:

The Even numbers in a given list :
4
6
10
12

filter() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Create a function say Even_numbers which accepts a number as an argument and returns true if the argument is an even number else it returns false.
  • Check if the given number is even using the if conditional statement.
  • If it is true, then return True.
  • Else return False.
  • Pass the above Function(Even_numbers),  given list as arguments to the filter function that returns an iterator in which the items are filtered through a function to determine whether or not the item is accepted.
  • Store it in another variable.
  • Loop in the above result using the for loop.
  • Print the iterator value of the for loop.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [3, 10, 12, 13, 20, 7, 1, 16]

# Create a function say Even_numbers which accepts a number as an argument and returns
# true if the argument is even number else it returns false.


def Even_numbers(numb):
  # Check if the given number is even using the if conditional statement.
    if numb % 2 == 0:
      # If it is true, then return True.
        return True
    # Else return False.
    else:
        return False


# Pass the above Function(Even_numbers),
# given list as arguments to the filter function that
# returns an iterator in which the items are filtered
# through a function to determine whether or not the item is accepted.
# Store it in another variable.
evn_numbrs = filter(Even_numbers, gvn_lst)
print("The Even numbers in a given list :")
# Loop in the above result using the for loop.
for n in evn_numbrs:
  # Print the iterator value of the for loop.
    print(n)

Output:

The Even numbers in a given list :
10
12
20
16
Using the Lambda Function Within a filter ()
gvn_lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# The lambda function returns True for odd numbers
oddnumbrs_itrtor = filter(lambda n: (n % 2 != 0), gvn_lst)

# converting to list
odd_numbrs = list(oddnumbrs_itrtor)
print("The Odd numbers in a given list:")
print(odd_numbrs)

Output:

The Odd numbers in a given list:
[1, 3, 5, 7, 9]

Inside the filter, we have directly passed a lambda function ().

For Odd numbers, our lambda function returns True. As a result, the filter() function returns an iterator that only contains Odd numbers.

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using map(),list(),input(),int() functions and store it in a variable.
  • Create a function say Even_numbers which accepts a number as an argument and returns true if the argument is an even number else it returns false.
  • Check if the given number is even using the if conditional statement.
  • If it is true, then return True.
  • Else return False.
  • Pass the above Function(Even_numbers),  given list as arguments to the filter function that returns an iterator in which the items are filtered through a function to determine whether or not the item is accepted.
  • Store it in another variable.
  • Loop in the above result using the for loop.
  • Print the iterator value of the for loop.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),int(),split() and map() functions
# and store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Create a function say Even_numbers which accepts a number as an argument and returns
# true if the argument is even number else it returns false.


def Even_numbers(numb):
  # Check if the given number is even using the if conditional statement.
    if numb % 2 == 0:
      # If it is true, then return True.
        return True
    # Else return False.
    else:
        return False


# Pass the above Function(Even_numbers),
# given list as arguments to the filter function that
# returns an iterator in which the items are filtered
# through a function to determine whether or not the item is accepted.
# Store it in another variable.
evn_numbrs = filter(Even_numbers, gvn_lst)
print("The Even numbers in a given list :")
# Loop in the above result using the for loop.
for n in evn_numbrs:
  # Print the iterator value of the for loop.
    print(n)

Output:

Enter some random List Elements separated by spaces = 4 6 7 9 10 12 11
The Even numbers in a given list :
4
6
10
12

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.