Python filter() function Usage [10+ Examples]

Related Searches:  python filter, python filter list, python filter function, filter list, list filter, python filter array, python3 filter, filter example, python array filtering, python filter string, python filter example, filter array python, python filter object, python list filter function, how to use filter function in python, how to use filter in python, python function filter, filter a list in python, print filter object python, how to filter in python, filter list python 3, python filter class, python how to use filter, filter python list, python filter method, filter list in python

 

Introduction to the python filter function

Python filter function is one of the programming primitives that we can use to filter data. It is a built-in python function that offers an elegant way to filter out all the elements of a sequence for which the function returns True using lambda expression.  The filter functions can be applied to an iterable such as a list, or a dictionary and create a new iterator that can filter out certain specific elements based on the condition that we provided. In this tutorial, we will learn about the python filter function where we will take different scenarios and use the filter function to filter the specific data. Moreover, we will also integrate the python filter function with other functions including map and reduce functions. Finally, we will also discuss some of the alternatives that we can use to replace the python filter function. By the end of this tutorial, you will have a solid command over python filter() function.

Advertisement

 

Getting start with Python filter function

Filtering is an operation consisting of testing each value in an iterable with a predicate function or condition and it retains only those values which are satisfied by the given condition. Filtering operations are fairly common in programming and most programming languages like python provide tools to approach them. A simplest example of filtering would be using if-else statements and gaining the required result. Suppose we have a list of numbers from 1 to 10 and we only want a list of odd numbers out of it. We can achieve this result by filtering the list using if-else statements. See the simplest example of filtering.

# list containing numbers
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]

# empty list
odd = []

# for loop to iterate
for i in list1:

   # condition for filtering
   if i%2!=0:
       odd.append(i)

# printing
print(odd)

Output:

[1, 3, 5, 7, 9]

But in python we don't have to use if-else statements every time for filtering. Python provides us with a built-in function known as fiter() which is used to filter data. The syntax of python filter function looks like this:

filter(function, iterable)

Now let us use python built-in function filter() to get the odd numbers from the list. Here is an example of it.

# list containing numbers
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]

# filtering function
def main(x):
   return x%2!=0

# python filter function
print(filter(main, list1))

Output:

<filter object at 0x7f7e5a608340>

We get an iterating object because the python filter function returns an iterable object. We can iterate over it to print the elements or we can just use a list method to iterate. See the example below:

Advertisement
# list containing numbers
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]

# filtering function
def main(x):
   return x%2!=0

# python filter function
print(list(filter(main, list1)))

Output:

[1, 3, 5, 7, 9]

 

Example-1: Python filter list of even numbers

Now let us find all the even numbers from a given list by filtering it. Our first approach might be to use an if statement with a for loop to iterate over a list of numbers and then append the even values in another empty list. See the example below:

# list containing numbers
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]

# empty list
even = []

# for loop to iterate over list
for i in list1:

   # filtering condition
   if i%2==0:
      even.append(i)
# printing
print(even)

Output:

[2, 4, 6, 8, 10]

In the example above, for loop iterates over the list and appends only even numbers to the empty list. The conditional statement plays the key role of filtering that tests every number to find out if it is even or not.

We can get the same result by using the python filter function as well. This way we can perform the same computation without using any explicit for loop: See the example of python filter function below:

# list containing numbers
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]

# filtering function
def main(x):

   # return even number
   return x%2==0

# python filter function
print(list(filter(main, list1)))

Output:

[2, 4, 6, 8, 10]

In the example above, the main() function takes an integer and returns True if it is even and False if it is not. The call to filter function does the hard work and filters out the even numbers. As a result, we get a list of even numbers. This code is shorter and more efficient than the for loop.

Example-2: Python filter list of prime numbers

Let us now take another example of using the python filter function. This time let us find all the prime numbers from a specified range of numbers.  First let us create a function that returns True if a number is prime, else return false. See the following example:

Advertisement
# importing math library
import math

# creating function
def main(x):

   # checking for negative numbers
   if x <= 1:
       return False
   #  for loop to iterate through number range to find its root if exist
   for i in range(2, int(math.sqrt(x)) + 1):
       if x % i == 0:
           return False

   # else return true
   return True
# calling function
print(main(9))
print(main(11))

Output:

False
True

Notice that 9 is not a prime number but 11 is and we get the result accordingly. Now let us use the python filter function to print all the prime numbers from a range of numbers. See the example below:

# importing math library
import math

# creating function
def main(x):

   # checking for negative numbers
   if x <= 1:
       return False

   #  for loop to iterate through number range to find its root if exist
   for i in range(2, int(math.sqrt(x)) + 1):
       if x % i == 0:
           return False

   # else return true
   return True

# using filter function
print(list(filter(main, range(30))))

Output:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

 

Example-3:  Validate python identifiers using python filter function

So far we have used the python filter function to iterate lists containing numeric data. We can also use it with iterables containing non-numeric data as well. For example strings or a list of strings. We can use the filter function to print out all the identifies from a list. But before doing that let us know what identifiers are in python?

A Python indefitier is simply a name used to identify a variable, function , class or other object. It can start with a letter, underscore followed by letters or numeric values. But python does not allow punctuation characters such as #, %, ! etc within identifiers. Python provides us a built-in function called isidentifier() that can help us to validate if a name is an identifier or not. By using this built-in function and filter we can check a list of names and validate which one is the identifier. See the example below:

# name of variables
variable_names = ["data", "88file", "x%x", "_non_", "school1", "1234"]

# python filter method
print(list(filter(str.isidentifier, variable_names)))

Output:

['data', '_non_', 'school1']

Notice that it only prints out three variable names which are valid python variable names.

 

Example-4: Find palindrome using python filter function

A palindrome word is a word that reads the same backward as forward. For example non is a palindrome word, because if we read it from backward, it will be the same again. To implement this in python and find out if a word is palindrome or not, we have to create  a function which applies the algorithm and returns true if a word is palindrome. See the following implementation of python code.

Advertisement
# creating function
def main(word):

   # storing backward of word
   backward = "".join(reversed(word))

   # check if backward is same as forward
   return word.lower() == backward.lower()

# calling function
print(main("Bashir"))
print(main("lol"))

Output:

False
True

Notice that the first word is not a palindrome while the second one is and we get the result, accordingly. Now we can apply the filter method by using this function to find out all the palindrome words from a list of words.  See the example below:

list1 =["alam", "madam", "alam", "alala", "we", "try", "Teet"]
# creating function
def main(word):

   # storing backward of word
   backward = "".join(reversed(word))

   # check if backward is same as forward
   return word.lower() == backward.lower()

# filter function
print(list(filter(main, list1)))

Output:

['madam', 'alala', 'Teet']

 

Use python filter function with other functions

So far we have learned how to use the filter method to run different filtering operations on iterables and come across various examples as well. For best practice, we can combine python filter function with other tools to perform different tasks on iterable without using explicit loops. In this section, we will discuss how we can use the python filter function to align with map(), reduce() and lambda function.

 

Python filter function and lambda()

A lambda expression is an anonymous, inline declaration of a function, usually passed as an argument. It can do anything  as the regular function does, except that it can not be called outside of the line where it was defined. It has no name and is quite useful when we require a short function.  One of its common applications is filtering data. Here is simple syntax of lambda expression in python:

lambda arguments: expression

Now let us use lambda expression in filtering data along with the python filter function. See the following example which filters all the even numbers using lambda expression.

# creating list
list1 = [1, 2,3, 4, 5, 6, 7, 8, 9, 10]

# filter using lambda expression
print(list(filter(lambda x: x%2==0, list1)))

Output:

[2, 4, 6, 8, 10]

In a similar way, we can customize the lambda expression to filter other data as well.

 

Python filter function and map() function

We may come across a situation where we need to take an iterable, process each of its items with a transformation function and produce a new iterable with the resulting items. In such cases using map( ) function along with python filter function will be ideal. Here is the simple syntax of the map function.

map ( function , iterables)

The map function applies the function to each item in the iterable to transform them into a different value with additional features. And return an iterable object. Let's say we want to find the cube of all the even numbers from a list. We can do this by using the filter and map function together. See the following example.

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# using map and filter function
print(list(map(lambda n: n ** 3, filter(lambda x: x%2==0, list1))))

Output:

[8, 64, 216, 512, 1000]

Notice in the example above that the filter function filters the list using lambda expression and returns an iterable object containing only even values. The map function takes that iterable object and lambda expression to give us a list of cubes of even values.

 

Python filter function and reduce() function

Another functional programming tool is reduce which is a built-in function which reduces an iterable to a single cumulative value. Such an operation is also known as reduction or folding. The syntax is simple syntax of reduce function look like this:

reduce(function, iterable, initial)

Now let us take an example which calculates all sum of all the odd numbers from a list using filter and reduce. See the example below:

# importing reduce
from functools import reduce

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# using reduce and filter function
print(reduce(lambda n, m: n + m, filter(lambda n: n%2==0, list1)))

Output:

30

 

Alternatives of python filter() function

We already discussed how filter, map and reduce functions help us to get the desired data from iterable. However, there are some other strong ways to achieve the same result. Kist comprehension and generator expressions have become strong and pythonic competitors in almost every use case.  The functionality of these functions provides almost more features and can be easily replaced with filter functions. In this section we will see how we can use list comprehension and generator expressions as an alternative to the python filter function.

 

Replace python filter() function with list comprehension

Python list comprehension is an easy and compact syntax for creating a list from a string or another list. It is a very concise way to create a new list by performing an operation on each item in the existing list. List comprehension is considerably faster than passing a list using the for loop. Here is simple syntax of list comprehension in python:

[expression for element in iterable if condition]

Now let us take an example and see how we can achieve the same result using list comprehension and python filter function. Let us first see the example of python filter to print all even numbers from a list.

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# using  filter function
print(list(filter(lambda x: x%2==0, list1)))

Output:

[2, 4, 6, 8, 10]

Now let us see how we can achieve the same result using list comprehension. See the example below:

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# using list comprehension
print([x for x in list1 if x%2==0])

Output:

[2, 4, 6, 8, 10]

See how simple the use of list comprehension is. It does not need to call any function, rather filter the data in one line.

Now let us see how we can get the square of each of the filtered even values. First let us take an example of a python filter function.

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# using map and filter function
print(list(map(lambda n: n ** 2, filter(lambda x: x%2==0, list1))))

Output:

[4, 16, 36, 64, 100]

Now let us perform the same logic using python list comprehension method. See the example below:

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# using list comprehension
print([x**2 for x in list1 if x%2==0])

Output:

[4, 16, 36, 64, 100]

 

Replace python filter() function with generator expression

Python generator expression is a more powerful tool to implement iterators. It is easy and more convenient to implement because it offers the evaluation of elements on demand. Unlike regular functions which on encountering a return statement terminate entirely, generators use yield statements in which the state of the function is saved from the last call and can be picked up or resumed the next time we call a generator function. Simple syntax of python generator expressions looks like this:

(expression for element in iterable if condition)

Now let us take an example of how we can use python generator expressions to filter data. First let us take an example using the python filter function. See the example which returns all the odd numbers from the list.

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# using filter
print(list(filter(lambda x: x%2!=0, list1)))

Output:

[1, 3, 5, 7, 9]

Now let us know how we can return all the odd numbers from the given list using generator expressions.

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# using generator expression
print(list((x for x in list1 if x%2!=0)))

Output:

 

[1, 3, 5, 7, 9]

Notice that the generator expressions are similar to list comprehension with little difference in syntax. Now let us return the square of all the odd values. First let us do the same logic in the python filter function. See the example below:

# creating list
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# using filter and map
print(list(map(lambda n: n ** 2, filter(lambda x: x%2!=0, list1))))

Output:

[1, 9, 25, 49, 81]

Now let us apply the same logic through generator expressions. See the example below:

 

Summary

The python filter function is a built-in function that filters the given sequence with the help of a function that tests each element in the sequence to be true or not. In this tutorial we learned about the python filter method, starting from getting familiar with its syntax to solving different examples using the filter function. We also covered how we can integrate the python filter function with other functions like map, reduce and lambda. Moreover, we also discussed some of the alternative ways of using filter function. All in all this tutorial covers each and everything that you need to know about python filter function.

 

Further Reading

Python filter method
filtering in python 
documentation of filter method

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment