Open In App
Related Articles

filter() in python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. 

Python filter() Syntax

The filter() method in Python has the following syntax:

Syntax: filter(function, sequence)

Parameters:

  • function: function that tests if each element of a sequence is true or not.
  • sequence: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.

Returns: an iterator that is already filtered.

Python filter Function Examples

Let us see a few examples of the filter() function in Python.

Python Filter Function with a Custom Function

In this example, we are using the filter function along with a custom function “fun()” to filter out vowels from the Python List.

Python

# function that filters vowels
def fun(variable):
    letters = ['a', 'e', 'i', 'o', 'u']
    if (variable in letters):
        return True
    else:
        return False
 
 
# sequence
sequence = ['g', 'e', 'e', 'j', 'k', 's', 'p', 'r']
 
# using filter function
filtered = filter(fun, sequence)
 
print('The filtered letters are:')
for s in filtered:
    print(s)

                    

Output:

The filtered letters are:
e
e

Filter Function in Python with Lambda

Python filter() function is normally used with Lambda functions. In this example, we are using the lambda function to filter out the odd and even numbers from a list.

Python3

# a list contains both even and odd numbers.
seq = [0, 1, 2, 3, 5, 8, 13]
 
# result contains odd numbers of the list
result = filter(lambda x: x % 2 != 0, seq)
print(list(result))
 
# result contains even numbers of the list
result = filter(lambda x: x % 2 == 0, seq)
print(list(result))

                    

Output:

[1, 3, 5, 13]
[0, 2, 8]

Filter Function in Python with Lambda and Custom Function

In this program, we will use both a custom function “is_multiple_of_3()” as well as a lambda function. The filter() function is used to apply this function to each element of the numbers list, and the lambda function is used to iterate over each element of the list before applying the condition. This way, we can perform additional operations on each element before applying the condition.

Python3

# Define a function to check
# if a number is a multiple of 3
def is_multiple_of_3(num):
    return num % 3 == 0
 
 
# Create a list of numbers to filter
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
# Use filter and a lambda function to
# filter the list of numbers and only
# keep the ones that are multiples of 3
result = list(filter(lambda x: is_multiple_of_3(x), numbers))
 
# Print the result
print(result)

                    

Output
[3, 6, 9]

Time complexity analysis

  1. The filter function is used to filter the list of numbers, and it applies the lambda function to each element of the list. The time complexity of the filter function is O(n), where n is the number of elements in the list.
  2. The time complexity of the lambda function is constant, O(1), since it only performs a single arithmetic operation. Therefore, the overall time complexity of the program is O(n).

Auxiliary Space analysis

The program uses a list to store the filtered numbers, so the space complexity is proportional to the number of filtered numbers. In the worst case, if all numbers are multiples of 3, the filtered list will have n/3 elements. Therefore, the space complexity is O(n/3), which simplifies to O(n) in big O notation.



Last Updated : 27 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads