How To Get The Length Of A ‘filter’ Object In Python

Get the length of a 'filter' object in Python

The length of the filter object is the number of items in the iterable object that pass the filter() function’s test. So, what is a filter object? This article will help you answer this question and demonstrate how to get the length of a ‘filter’ object in Python.

What is a filter object in Python?

An object created by the built-in filter() function is a filter object. The filter object contains elements that have passed a function test specified in the filter() function. And we can work with the elements of the filter object by converting it to a list.

How can you get the length of a ‘filter’ object in Python?

Here are three approaches we’d like to share with you. Don’t just read it; try to apply it to your situation.

Using the len() function

The len() function returns the number of items in an object.

To get the length of a filter object, we must first create one with the filter() function. Then we will convert the filter object to a list using the list() function. Finally, we use the len() function to determine the length. For a better understanding, consider the example below.

listOfWords = ["Learn", None, "Python", None, "at", "LearnShareIT"]

# Use the filter() function to create a filter object
ftObject = filter(lambda value: value is not None, listOfWords)

# Convert the filter object to a list
ftList = list(ftObject)

# Use the len() function to get the length of ftList
ftLen = len(ftList)
print(f"The length of the filter object is {ftLen}")

Output:

The length of the filter object is 4

Using For loop

You can also use the For loop to determine the length of a filter object. You must first create a variable and set it to 0. Then, you need to loop through each item in the filter object and add 1 to the variable for each item. Finally, print the variable out. For example:

listOfWords = ["Learn", None, "Python", None, "at", "LearnShareIT"]

# Use the filter() function to create a filter object
ftObject = filter(lambda value: value is not None, listOfWords)

# Create a counter variable
ftLen = 0

for item in ftObject:
  # Add 1 to the counter for each item
  ftLen += 1

print(f"The length of the filter object is {ftLen}")

Output:

The length of the filter object is 4

Using the ilen() function

Syntax:

ilen(iterable)

Description:

The ilen() function returns the length of the iterable.

This function is a function in the More-Itertools library. As a result, we have to install the library with the command line:

pip install more-itertools

With ilen(), we can get the filter object’s length without converting it to a list. That is the advantage of ilen() over len(). For instance:

from more_itertools import ilen

listOfWords = ["Learn", None, "Python", None, "at", "LearnShareIT"]

# Use the filter() function to create a filter object
ftObject = filter(lambda value: value is not None, listOfWords)

# Use the ilen() function to get the length of the ftObject without having to convert it to a list
ftLen = ilen(ftObject)

print(f"The length of the filter object is {ftLen}")

Output:

The length of the filter object is 4

Summary

We have explained what a filter object is and demonstrated three methods to get the length of a ‘filter’ object in Python. This task is not difficult, and we can complete it quickly. You can apply the knowledge in this article to other data types, not just filter objects.

Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *