How To Resolve TypeError: Object Of Type ‘generator’ Has No len() In Python

TypeError: object of type 'generator' has no len() in Python

To fix TypeError: object of type ‘generator’ has no len() in Python, you can use the more_itertools.ilen() function, convert the generator to a list, or use the isinstance() function. Read the detailed article below.

What causes the TypeError: object of type ‘generator’ has no len() in Python?

This error happens because you use the len() function to get the length of a generator.

Example:

def myGenerator():
    yield 'John'
    yield 'Frank'
    yield 'Peter'
    
# Use the len() function to get length of generator
lengthGen = len(myGenerator)
print(lengthGen)

Output

Traceback (most recent call last):
  File "d:\vscode\Python\gen.py", line 7, in <module>
    lengthGen = len(myGenerator)
                ^^^^^^^^^^^^^^^^
TypeError: object of type 'function' has no len() 

How to solve the TypeError: object of type ‘generator’ has no len() in Python?

Use the more_itertools.ilen() function from the more_itertools package

Syntax:

more_itertools.ilen(iterable)

The more_itertools.ilen() function returns the number of items in iterable.

Example:

  • Import the more_itertools module.
  • Create a generator.
  • Use the more_itertools.ilen() function to get the length of the generator.
import more_itertools

def myGenerator():
    yield 'John'
    yield 'Frank'
    yield 'Peter'
    
# Use the more_itertools.ilen() function to get the length of the generator
print('Length of generator:', more_itertools.ilen(myGenerator()))

Output:

Length of generator: 3

Note: If you get the ModuleNotFoundError: No module named ‘more_itertools’, then install the more_itertools package as follows: pip install more-itertools.

Use the len() function

Example:

  • Create a generator.
  • Use the list() function to convert the generator to a list.
  • Use the len() function to get the length of the list or the length of the generator itself.
def myGenerator():
    yield 'John'
    yield 'Frank'
    yield 'Peter'
  
myGen = myGenerator()
 
# Use the list() function to convert the generator to a list
convertToList = list(myGen)
 
# Use the len() function to get the length of the list (the generator converts to a list)
print('Length of generator:', len(convertToList))

Output:

Length of generator: 3

Or you can use a for loop to get the length.

Example:

def myGenerator():
    yield 'John'
    yield 'Frank'
    yield 'Peter'
    
myGen = myGenerator()

# Use the list() function to convert the generator to a list
convertToList = list(myGen)

# Use the for loop to get the length of the list or the length of the generator itself
counter = 0
for item in convertToList:
	counter+=1

print('Length of generator:', counter)

Output:

Length of generator: 3

Use the isinstance() function

Example:

  • Note: Use GeneratorType from types.
  • Use the isinstance() function to check if ‘myGenerator’ is a generator, then execute the statements in the if statement.
import types
 
def myGenerator():
    yield 'John'
    yield 'Frank'
    yield 'Peter'
    
myGen = myGenerator()
 
# Use the isinstance() function to check
if isinstance(myGen, types.GeneratorType) is True:  
    # Use the list() function to convert the generator to a list
    convertToList = list(myGen)
    print('Length of generator:', len(convertToList))

Output:

Length of generator: 3

Or use the type() function.

Example:

import types
 
def myGenerator():
    yield 'John'
    yield 'Frank'
    yield 'Peter'
    
myGen = myGenerator()
 
# Use the type() function to check
if type(myGen) is types.GeneratorType:  
    # Use the list() function to convert the generator to a list
    convertToList = list(myGen)
    print('Length of generator:', len(convertToList))

Output:

Length of generator: 3

Summary

Those are the ways to fix the TypeError: object of type ‘generator’ has no len() in Python. You can take advantage of Python’s convenience by using the built-in more_itertools.ilen() function.  If you have any questions about the article, leave a comment below.

Maybe you are interested:

Leave a Reply

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