In python, the error “TypeError: object of type ‘int’ has no len()” occurs when the length function is invoked on objects that does not support the length function. The length function is supported by iterable objects such as list, tuple, dictionary and string.
The length function is used to find the length of the string or the number of objects in a structured objects such as list, tuple and dictionary. The length function does not supported by the data types such as int, float, long, boolean, complex data types. If the length function is invoked on unsupported objects, the python interpreter will cause the error.
The purpose of the len() function is to determine the length of the list, tuple, dictation, or string. In this article, we can see what this error is, how this error can be resolved. The error would be thrown as like below.
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print (len(s))
TypeError: object of type 'int' has no len()
[Finished in 0.2s with exit code 1]
Different Variation of the error
In different contexts the “TypeError: object of type ‘int’ has no len()” error is thrown in various forms in python. The numerous variations of TypeError are shown below.
TypeError: object of type 'NoneType' has no len()
TypeError: object of type 'int' has no len()
TypeError: object of type 'float' has no len()
TypeError: object of type 'long' has no len()
TypeError: object of type 'bool' has no len()
TypeError: object of type 'complex' has no len()
TypeError: object of type 'type' has no len()
TypeError: object of type 'builtin_function_or_method' has no len()
Root Cause
The root cause of this error is that the len() function is invoked on objects that are not supported to calculate the length. Iterable objects such as list, tuple, dictionary, and strings are supported to find the length of the elements in it.
The len() function does not make sense for types such as integer, float, boolean, long, complex. The Python interpreter will throw this error when the len() function is invoked on this.
How to reproduce this error
If the len() function is invoked for the unsupported objects such as int, float, boolean, long etc, this error will be thrown. calling the len() function for the unsupported objects to reproduce this error “TypeError: object of type ‘int’ has no len()” in python.
s=5
print (len(s))
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print (len(s))
TypeError: object of type 'int' has no len()
[Finished in 0.2s with exit code 1]
Solution 1
The len() function should only be used for iterable objects such as list, tuple, dictionary and string. Make sure you return any of the objects. Changing the primary data types to the iterable objects will resolve this error “TypeError: object of type ‘int’ has no len()” in python.
s = [5,4]
print (len(s))
Output
2
Solution 2
The len() function must be called before checking the object type. If the object is an iterable object, such as a list, tuple, dictionary, or string, the len() function will be called. Otherwise the error message “TypeError: object of type ‘int’ has no len()” will be shown to the user that the length of the object can not be found.
s=[2]
print type(s)
if type(s) in (list,tuple,dict, str):
print (len(s))
else:
print "not a list"
Output
1
Solution 3
The length can not be determined if the argument for the len() function is None. The value of the argument must be checked for non-None before calling the len() function. The error message “TypeError: object of type ‘NoneType’ has no len()” will be shown as in the example below.
s=None
print (len(s))
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print (len(s))
TypeError: object of type 'NoneType' has no len()
[Finished in 0.1s with exit code 1]
Solution
s=None
print type(s)
if s is None :
print "a None value"
else:
print (len(s))
Output
a None value
Solution 4
The length can not be determined if the argument for the len() function is a python type. The value of the argument must be checked for type before calling the len() function or default value is set. The error message “TypeError: object of type ‘type’ has no len()” will be shown as in the example below.
s=list
print len(s)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print len(s)
TypeError: object of type 'type' has no len()
[Finished in 0.0s with exit code 1]
Solution
s=list()
print len(s)
Output
0
Solution 5
The length can not be determined if the argument for the len() function is a python build in function. The value of the argument must be checked before calling the len() function. The error message will be shown as in the example below.
s=len
print len(s)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print len(s)
TypeError: object of type 'builtin_function_or_method' has no len()
[Finished in 0.1s with exit code 1]
Solution
s=(2,3)
print len(s)
Output
2