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

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

While programming, you have probably encountered the TypeError: object of type ‘map’ has no len() in Python. We have some solutions that can work around this error, like changing the parameter object to the len() function and checking the map object. Read the article for more details.

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

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type.

In short, the TypeError: object of type ‘map’ has no len() happens because the len() function does not accept the map object as a parameter to the function.

Note: The len() function will accept the following objects as arguments to a proper function: string, bytes, tuple, list, range, dictionary, set, or frozen set.

Example: 

mySet = {1, 2, 3}

# Map object
myMap = map(int, mySet)

print(len(myMap))

Output:

Traceback (most recent call last):
  File "./prog.py", line 6, in <module>
TypeError: object of type 'map' has no len()

How to solve this error?

Convert map object ​​to other objects

As I should have shown the objects that the len() function can take as a parameter, you only need to change the map object to valid objects by the len() function.

Example:

  • Initialize a map object.
  • Convert the map object to the correct types as the len() function parameter. I take a few styles as examples.
  • Use the len() function to get the length of that object.
originSet = {1, 2, 3}

# The map object is converted to a set
mySet = set(map(int, originSet))

# The map object is converted to a list
myList = list(map(int, originSet))

# The map object is converted to a tuple
myTuple = tuple(map(int, originSet))

print('The length of set is: ', len(mySet))
print('The length of list is: ', len(myList))
print('The length of tuple is: ', len(myTuple))

Output:

The length of set is: 3
The length of list is: 3
The length of tuple is: 3

Use the isinstance() function

Syntax:

isinstance(Object, Classinfo)

Parameters:

  • Object: the object you want to check.
  • Classinfo: class, type or tuple.

Example:

  • Initialize a map object.
  • Use the isinstance() function to check if the number is a map object.
originSet = {1, 2, 3}

# Map object
myMap = map(int, originSet) 

if isinstance(myMap, map):
	print("Value is map object")
else:
	print('Object length: ', len(myMap))

Output:

Value is map object

Use the type() function to check the object

Example:

  • Initialize a map object.
  • Use the type() function to check if objects are data types that can be len() function parameters, then print the length of those objects. Do not execute the ‘else’ command.
mySet = {1, 2, 3}

# Map object
myMap = map(int, mySet)

if type(myMap) in (str, bytes, tuple, list, range, dict, set, frozenset): 
	print(len(myMap))
else:
	print('The len() function does not accept this data type.')

Output:

The len() function does not accept this data type.

Summary

You should convert the map object ​​to other objects to fix the TypeError: object of type ‘map’ has no len() in Python. Hope you make it!

Maybe you are interested:

Leave a Reply

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