Python Filter Function

The filter is a built-in Python function. It filters the specified sequence or iterable through a function that checks each item or element in the iterable to be true or false. To put it simply, the filter function filters the iterable through a function. The given function test each item of the sequence or iterable and returns the true if the item satisfied the given criteria. As a result, the filter() function constructs a new iterator. The most commonly used iterable are lists, tuples, and dictionaries. This article briefly describes the filter() function with the help of examples.

Syntax of filter() function

Let’s discuss the syntax of filter() function first. The syntax of the filter() function is as follows:

filet(function, iterable or sequence)

The filter function takes two parameters as an argument, i.e., function and an iterable. The function checks every element or item of the iterable, whether it is true or not, according to given criteria. The second argument is iterable or sequence to be filtered. The function evaluates each item of the iterable on the given criteria. Finally, the filter() function returns the filtered iterator.

Let’s see the examples of the filter() function and implement it in our Python script. We are using a spyder3 editor to create and run the Python scripts.

Example 1: Filter the list of numbers

Let’s create the list of numbers and filter it. In the given example, the list_func() evaluates every list item on a given condition. If the list item is greater than 5, then the function returns true and adds the item to the filter.

#creating a list of number

my_list = [1,2,3,4,5,6,10,20,35,8,4,50,60]

#creating a function

def list_func(listObj):

    # a conditional statement to check the number

    if(listObj>5):

        return True

    else:

        return False

#using the filter function to filter the iterable through list_func

numbers = filter(list_func,my_list)

#printing the new iterator

for i in numbers:

    print(i)

Output
The output displays the filtered iterator. The filtered iterator is constructed by all those items which are greater than 5.

Example 2: Filter the list of characters

Let’s create the list of characters and apply the filter function.

#creating a list of number

my_list = ['a','b','c','l','d','i','k','n','o','u','x']

#creating a function

def list_func(listObj):

    word_list=['l', 'i', 'n', 'u', 'x']

    # a conditional statement to check the character

    if(listObj in word_list):

        return True

    else:

        return False

#using the filter function to filter the iterable through list_func

words = filter(list_func,my_list)

#printing the new iterator

for i in words:

    print(i)

Output

Examples 3: Filter the even number

Let’s filter the even numbers from the given list of numbers using the filter function.

#creating a list of number

my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

#creating a function

def list_func(listObj):

    # a conditional statement to check the even number

    if(listObj%2==0):

        return True

    else:

        return False

#using the filter function to filter the iterable through list_func

even_numbers= filter(list_func,my_list)

#printing the new iterator

for i in even_numbers:

    print(i)

Output

Examples 4: Filter the leap year

Let’s filter the leap years from the given list of years using the filter function.

#creating a list of number

my_list = [2010,2012,2014,2016,2018,2020,2022,2024]

#creating a function

def list_func(listObj):

    # a conditional statement to check the leap year

    if(listObj%4==0):

        return True

    else:

        return False

#using the filter function to filter the iterable through list_func

leap_year= filter(list_func,my_list)

#printing the new iterator

for i in leap_year:

    print(i)

Output

The output displays the leap years from the given list of years.

Examples 5: using lambda function with filter()

The filter function is mostly used with lambda function. Let’s pass the lambda function as an argument to filter() function.

#creating a list of number

my_list = [2010,2012,2014,2016,2018,2020,2022,2024]

#declaring an even_number variable

#even_number variable contains the even numbers

#using lambda function

even_number = filter(lambda listObj:listObj%2==0,my_list)

print("The list of even numbers")

for i in even_number:

    print(i)

leap_year = filter(lambda listObj:listObj%4==0,my_list)

print("The list of leap years")

for x in leap_year:

    print(x)

Output

Conclusion

The filter() is a Python built-in function that is used to filter the iterable on a given function. This article explains the filter() function with examples.



from Linux Hint https://ift.tt/32talvr

Post a Comment

0 Comments