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.

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee