The TypeError object of type NoneType has no len() error occurs when we tried to apply the len() method on NoneType objects. The len() method works on iterable objects such as lists, tuples, dictionaries, and strings because they contain a sequence of elements. This short article will discuss how we can solve TypeError: object of type ‘NoneType’ has no len() error using various methods. Moreover, we will also cover how to understand errors in Python so that in the future we can easily solve the issues.

If the given solution does not apply to you and couldn’t solve your problem, please let us know through comments and we will try to help you.
Solve TypeError: object of type NoneType has no len() in Python
TypeError: object of type ‘NoneType’ has no len() error occurs when we tried to apply the len() method to a NontType object. In Python, the len method can be only applied to iterables which include strings, lists, dictionaries, and tuples. Because they contain the sequence of elements. If you are getting TypeError: object of type ‘NoneType’ has no len() error, make sure that you are applying the len function on iterable:
Let us take an example and see why we are getting TypeError: object of type ‘NoneType’ has no len() error:
# defining a nonetype
f = None
# using len() method
len(f)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_156831/2732592659.py in <module>
3
4 # using len() method
----> 5 len(f)
TypeError: object of type 'NoneType' has no len()
Notice that we got the error because we tried to apply the len method on a NoneType object which is not iterable.
There can be many possible reasons for getting a NoneType object. Some of the main reasons for getting a None Type object might be:
- Defining a function that does not return anything which means it actually returns None.
- Maybe you have set the value of the variable as None.
- Maybe you have assigned a value returned by a built-in function to the variable and the function does not return anything.
- Maybe you have a function that returns iterable only if the condition is satisfied.
In any case, let us solve the problem:
Solution-1: Function that returns nothing
As we discussed that one of the reasons for getting this error can be, we might have a function that is not returning anything. Sometime. we create a function but forget to return anything or even if returned something, we don’t check the returned value. It is highly recommended to check the returned value of the function before calling it.
For example, see the example below where the function does not return any specific value and when applying the len(), we get error:
# defining a function
def main():
print([1,2,3,4,5,6])
# calling the function
list1 = main()
# using len() method
print(len(list1))
[1, 2, 3, 4, 5, 6]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_156831/4079716561.py in <module>
7
8 # using len() method
----> 9 print(len(list1))
TypeError: object of type 'NoneType' has no len()
The main reason for getting this error is that the function does not return anything and by default, the return is None Object.
To solve the issue, you can check the return type of the function:
# defining a function
def main():
print([1,2,3,4,5,6])
# checking the returning type
type(main())
[1, 2, 3, 4, 5, 6]
NoneType
Notice that the return type of the function is None. The reason for having a NoneType return value of the function is that the function is not returning any value.
To solve the problem, we need to return something from the function. In this case, we will return the list, instead of printing it:
# defining a function
def main():
return [1,2,3,4,5,6]
# calling the function
list1 = main()
# using len() method
print(len(list1))
6
Note that we were able to handle and solve the error caused by NoneType returned by the function.
Solution-2: Use the if statement to check the return type of variable
Another alternative to solve the issue caused by NoneType returned by the function is to use the if statement to check the return type of the function:
For example, see the Python code below:
# defining a nonetype
f = None
# checking the type
if f is None:
print("the variable is none")
# else statement
else:
len(f)
the variable is none
As you can see, because the type of the variable was None, the if statement has been executed.
Solution-3: NoneType from the built-in function
Sometimes, the built-in methods can also return a NoneType object, and applying len() method can cause TypeError: object of type ‘NoneType’ has no len().
For example, let us say that we have a list of string values and when we apply the sort() method, then it will return a None type:
# creating a list
list1 = ['1', '2' ,'3', '5', '0']
# applying the sort method
sort = list1.sort()
# print
print(len(sort))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_156831/121406387.py in <module>
6
7 # print
----> 8 print(len(sort))
TypeError: object of type 'NoneType' has no len()
The reason for getting TypeError: object of type ‘NoneType’ has no len() error is because string values in a list cannot be sorted and the returned value is NoneType.
So, make sure that returned value from a built-in method is None. You can use the if-statement to check if the value is None or not:
# creating a list
list1 = ['1', '2' ,'3', '5', '0']
# applying the sort method
sort = list1.sort()
if sort is None:
print("The returned value is none")
else:
len(sort)
The returned value is none
As you can see, we handled the error using the if statement.
Solution-4: Using try-except blocks in Python
The try-except blocks in Python are used to handle errors. The try block will be executed first and if there is an error, then the except block will be executed.
Let us see, how we can use the try-except block to handle TypeError: object of type ‘NoneType’ has no len() error in Python:
# creating a list
list1 = ['1', '2' ,'3', '5', '0']
# applying the sort method
sort = list1.sort()
try:
len(sort)
except:
print("there is an error in the try block")
there is an error in the try block
As you can see, we handled the error successfully.
Understanding the TypeError: the object of type NoneType has no len() error
Let us now understand the error. In Python, the errors usually have two main parts. The first part of the error represents the category of the error which in this case is the TypeError. Which simply means we are performing some function on the wrong data type. The second part of the error gives more specific information about the error and in this case, it explains what kind of TypeError is there. It clearly says, object with NoneType has no len() method which means we are applying the len() method on NoneType objects somewhere in our code.
What is NonType in Python?
NoneType in Python is a data type that represents no value to the object or an empty object. In Python, we can use the None keyword to define a NonType object. Let us create a NonType object in Python using the None Keyword:
# creating an object
var = None
# printing the type
print(type(var))
<class 'NoneType'>
As you can see, the type of the object is NonType.
How does the len() method work in Python?
The len() method in Python is used to find the length of the sequential data structure. For example, it can be used to find the total length of a list, string, or tuple.
See, the following example which uses the len() method to find the length of the data structures:
# variables
list1 = [1,2,3,4,5,6,7,8,9]
string = "Bashir alam"
# printing
print(len(list1))
print(len(string))
9
11
As you can see, we got the length of the list and the string.
Summary
In this short article, we discussed how we can solve TypeError: object of type ‘NoneType’ has no len() error. We discussed four different methods to solve TypeError: object of type ‘NoneType’ has no len() error along with examples. We understand that the error occurs when we try to use the len() method on a NonType object. A NoneType object is an object without any value. Moreover, we also covered how to understand errors in Python:
Related Articles
- IndentationError: expected an indented block in Python
- AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’
- TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’
- ModuleNotFoundError: No module named ‘bs4’ [Solved]
- Typeerror: first argument must be callable or none