TypeError: object of type ‘NoneType’ has no len() in Python

None object in Python has a data type which is referred to as “NoneType”. The None value is not equal to “0” it is equal to None/Null because it has a specific memory size in Python. The len() function in Python is utilized to get the length of any input variable other than None. If None is passed inside the len() function, it will return a “TypeError ”. To resolve this error, various functions are used in  Python.

This write-up will provide various reasons and solutions for “TypeError: object of type NoneType has no len()” in Python. The following aspects will be discussed in this guide:

So, let’s get started!

Reason 1: Assigning None Value to len() Function

One of the prominent reasons that invokes the “object of type NoneType has no len()” error is when the “None” value is assigned to the len() function in Python. An example snippet that shows the stated error is given below:

How to install Kali Linux in Window...

The output shows a TypeError that occurs when the len() function tries to calculate the length of the none-type variable.

Solution 1: Change the None Value

To solve this error, locate the variable where the “None” type value is assigned and change it to some other value.

Code:

value = [1,2,3,4,5]

len_value = len(value)

print(len_value)

In the above code, a list is created instead of a none-type value. The list variable is passed to the “len()” function; consequently, it returns the length of the list.

Output:

The above output successfully calculates the length of the input variable.

Solution 2: Using an if-else Statement

Another way to resolve the specified error is by using an “if-else” statement in the program. The example code of this solution is shown below:

Code:

list_value = None

if list_value is not None:
    print(len(list_value))
else:
    print('None value Found in List')

In the above code, the “None” value is assigned to a variable named “list_value”. The “if” statement determines whether a variable has a value of “None”. if the variable has a “None” value, then the “if” condition becomes “False”, and the “else” block will execute.

Output: 

The above output shows that the given variable has a “None” value.

Reason 2: Function Returns None

The stated error occurs when the user-defined function returns the “NoneType” value. An example of this is shown below; let’s look:

The above-given error snippet shows that the function returns a “None” value, due to which the TypeError arises in the output.

Solution: Use return Statement

To rectify this error, we simply need to return some value to the function because when the function is accessed, the return value will be executed.

Code:

def examples():
    return([1, 22, 33])

list_val = examples()

print(len(list_val))

In the above code, the “return” statement is used to return the value to a function named “examples”. The “len()” function accepts the return value as an argument and determines the length.

Output:

The above output shows that the return statement calculated the length of the given list.

Reason 3: Function returns None if Condition Not Satisfied

This “TypeError” also appears at the output when the condition written inside the function is not fulfilled. For instance, look at the following snippet:

The above output shows the error because the condition inside the function is not satisfied.

Solution: Initialize an Empty List Inside the Function

One approach to resolve this error is by returning an empty list to the function. When the empty list is returned, the value will be “zero” at the output.

Code:

def examples(num):
    if len(num) > 3:
        return num
    return []

list_val = examples([5, 7, 8])

print(len(list_val))

In the above code, the user-defined function is defined and the “if” statement executes its block of code when the condition is satisfied. But when the condition is not satisfied, an empty list will be returned back to the function.

Output:

The output proves that when the specified condition becomes false, the function retrieves 0 instead of throwing a TypeError.

Reason 4: sort() Method Return None

Some of the built-in methods in Python return a None value, such as the “sort()” method. The “sort()” method will update the existing list and return None.

The above output successfully sorts the given list; however, it throws an error when the len() function accepts a variable that holds the sorted list.

Solution: Find the Length of the Original List Instead of the Sort list

To solve this error, we simply need to input the original list variable inside the “len()” function rather than the variable of the sorted list.

Code:

list_val = [44,12,56,76,88]

sort_value=list_val.sort()

print(list_val)

print(len(list_val))

In the above code, the len() function takes the original list variable named “list_val” as an argument instead of the sort list variable named “sort_value”.

Output:

The above output shows the sorted list and the length of the sorted list.

That’s it from this guide!

Conclusion

The “object of type NoneType has no len()” error occurs when the “None” value is passed inside the “len()” function in Python. To resolve this error, various solutions are used in Python, i.e., changing the “NoneType” value, using an if-else statement, using a return statement, etc. The best way to resolve this error is to locate the variable/function where the “None” type value is assigned and change it to another value. The “if-else” statement is also used to check whether the input variable has a “None” value. This Python article presented various reasons and respective solutions for the “object of type NoneType has no len()” error, along with appropriate examples.