How to Solve Python TypeError: object of type ‘map’ has no len()

by | Programming, Python, Tips

This error occurs when you try to pass a map object to a len() method call. The map() function executes a specified function for each item in an iterable and returns a map object, which is an iterator. In Python, iterators do not have a length.

You can solve this error by converting the map object to a list object using the built-in list() method. Then you can pass the list as the argument to the len() method. For example,

def square(i):
    res = i ** 2
    return res

lst = [2, 3, 4, 5]

squared = list(map(square, lst))

print(len(squared))

This tutorial will go through the error in detail and how to solve it with code examples.


TypeError: object of type ‘map’ has no len()

We raise a Python TypeError when attempting to perform an illegal operation for a specific type. In this case, the type is map.

The part ‘has no len()‘ tells us the map object does not have a length, and therefore len() is an illegal operation for the map object.

Retrieving the length of an object is only suitable for iterable objects, like a list or a tuple. A map object is an iterator containing values returned by the function applied by the map() function to a specified iterable. All iterators have the __iter__ dunder method, which we can check by passing the iterator to the dir() method:

def cube(i):
    res = i ** 3
    return res

lst = [2, 3, 4, 5]

cubed = map(cube, lst)

print(type(cubed))

print('__iter__' in dir(cubed))
<class 'map'>
True

The len() method implicitly calls the dunder method __len__() which returns a positive integer representing the length of the object on which it is called. All iterable objects have __len__ as an attribute. Let’s check if __len__ is in the list of attributes for the map object and the list object using the built-in dir() method.

def cube(i):
    res = i ** 3
    return res

lst = [2, 3, 4, 5]

cubed = map(cube, lst))

print(type(cubed))

print('__len__' in dir(cubed))
<class 'map'>
False

We can see that __len__ is not present in the attributes of the map object.

lst = ["Spinach", "Carrot", "Potato"]

print(type(lst))

print('__len__' in dir(lst))
<class 'list'>
True

We can see that __len__ is present in the attributes of the list object.

Example

Let’s look at an example of trying to get the length of a map object. In the following code, we will define a function which takes a number and returns it square.

def square(i):
    res = i ** 2
    return res

Then we will use the built-in map function to apply the function to a list of integers.

lst = [2, 3, 4, 5]

squared = map(square, lst)

Finally, we will attempt to get the length of the map object.

print(len(squared))

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 print(len(squared))

TypeError: object of type 'map' has no len()

The error occurs because the map object is an iterator and does not have a length.

Solution

We can solve the error by converting the map object to a list, which is an iterable data type. We can convert a map object to a list using the built-in list() method. Let’s look at the revised code:

def square(i):
    res = i ** 2
    return res

lst = [2, 3, 4, 5]

squared = list(map(square, lst))

print(len(squared))

Let’s run the code to get the length of the list:

4

We successfully converted the map object to a list and then used the len() method to get the length of the list.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on the has no len() TypeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python, which provides the best, easy-to-use online courses.

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!