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

Posted in /  

Python TypeError: object of type 'NoneType' has no len()
vinaykhatri

Vinay Khatri
Last updated on December 14, 2024

    The len() is an inbuilt Python function that returns the total number of elements or characters present in an iterable object, such as string, list, tuple, set, or dictionary. And if we try to perform the len() function on a non-iterable object like None, there, we will encounter the error " TypeError: object of type 'NoneType' has no len() ".

    In this Python error debugging tutorial, we will discuss why this error occurs in a Python program and how to solve it. To learn this error in detail, we will also walk through some common example scenarios, so you can solve the error for yourself.

    So without further ado, let's get started with the error statement itself.

    Python Problem TypeError: object of type 'NoneType' has no len()

    In Python, every data value has a data type that we can find using the type() function. The integer values like 1, 2, 3, etc., have a data type of int , floating-point numbers 1.0, 2.3, 4.34, etc. have the data type of float . Similarly, the None value data type is NoneType . We can confirm it using the type function.

    >>> type(None)
    <class 'NoneType'>

    Now let's take a look at the error statement. The error statement ypeError: object of type 'NoneType' has no len() has two parts.

    1. TypeError
    2. object of type 'NoneType' has no len()

    TypeError

    TypeError is one of the most common Python standard exceptions. It is raised in a Python program when we perform an invalid or unsupported operation on a Python data object.

    object of type 'NoneType' has no len()

    The statement " object of type 'NoneType' has no len() " is the error message telling us that the data type ' NoneType ' does not support the len() function.

    Error Reason

    We only get this error in a Python program when we pass a None value as an argument to the len() function.

    Example

    value = None
    
    print(len(value))
    Output
    TypeError: object of type 'NoneType' has no len()

    Common Example Scenario

    Now we know why this error occurs in a Python program, let's discuss some common example scenarios where many python learners encounter this error.

    1. Reassign the list with None returning methods.
    2. Forget to mention the return statement in a function.

    1. Reassign the list with None returning Method

    There are many methods in the list that perform the in-place operations and return None. And often, when we do not have a complete idea about the return value of the list methods , we assign the returned None value to the list name and perform the len() operation on the newly assigned value, and receive the error.

    Error Example

    Let's say we have a list bucket that contains the name of some items, and we want to sort that list in alphabetical order.

    bucket = ["Pen", "Note Book", "Paper Clip", "Paper Weight", "Marker"]
    
    # sort the bucket 
    bucket = bucket.sort()   #None
    
    items = len(bucket)
    print("There are total", items, "items in your bucket")
    
    for item in bucket:
    	print(item)

    Output

    Traceback (most recent call last):
      File "main.py", line 6, in 
        items = len(bucket)
    TypeError: object of type 'NoneType' has no len()

    In the above example, we are receiving the with the statement len(bucket) . This is because in line 4, where we have sorted the list " bucket " there, we have also assigned the bucket.sort() to bucket .

    The list sort() method performs the in-place sorting and returns None as a value.  And when we assigned the sort() method's returned value to bucket in line 4, where the value of the bucket became None . Later, when Python tries to perform the len() function on the None bucket value, Python raises the error.

    Solution

    For those methods that perform an in-place operation such as sort() we do not need to assign their return value to the list identifier. To solve the above problem, all we need to take care of is that we are not assigning the value returned by the list sort() method.

    bucket = ["Pen", "Note Book", "Paper Clip", "Paper Weight", "Marker"]
    
    # sort the bucket 
    bucket.sort()  
    
    items = len(bucket)
    print("There are total", items, "items in your bucket")
    
    for item in bucket:
    	print(item)

    Output

    There are total 5 items in your bucket
    Marker
    Note Book
    Paper Clip
    Paper Weight
    Pen

    2. Forget to mention the return statement in a function

    A function also returns a None value if the interpreter does not encounter any return statement inside the function.

    Error Example

    Let's say we are creating a function that accepts a string value and remove all the vowels from the string.

    # function to remove vowels
    def remove_vowels(string):
    	new_string = ""
    	for ch in string:
    		if ch not in "aeiou":
    			new_string += ch
    
    string = "Hello Geeks Welcome to TechGeekBuzz"
    
    new_string = remove_vowels(string)   #None
    
    string_len = len(string)
    
    new_string_len = len(new_string)   #error
    
    print("The Length of actual string is:", string_len)
    
    print("The Length of new string after vowels removal is:", new_string_len)

    Output

    Traceback (most recent call last):
      File "main.py", line 14, in 
        new_string_len = len(new_string)   #error
    TypeError: object of type 'NoneType' has no len()

    In this example, we are getting the error in line 14 with the new_string_len = len(new_string) statement. In line 14, we are trying to get the length of the new_string value that we have computed with the function remove_vowels() . We are getting this error because in line 14, the value of new_string is None .

    Solution

    To debug the above example, we need to ensure that we are returning a value from the remove_vowels() function, using the return statement.

    # function to remove vowels
    def remove_vowels(string):
    	new_string = ""
    	for ch in string:
    		if ch not in "aeiou":
    			new_string += ch
    	
    	return new_string
    
    string = "Hello Geeks Welcome to TechGeekBuzz"
    
    new_string = remove_vowels(string)   
    
    string_len = len(string)
    
    new_string_len = len(new_string)   
    
    print("The Length of the actual string is:", string_len)
    
    print("The Length of the new string after vowels removal is:", new_string_len)

    Output

    The Length of the actual string is: 35
    The Length of the new string after vowels removal is: 23

    Conclusion

    The Python len() function can only operate on iterable objects like a list, tuple, string, dictionary, and set. If we try to operate it on a NoneType object , there we encounter the error "TypeError: the object of type 'NoneType' has no len()".

    To debug this error, we need to ensure that the object whose length we are trying to find using the len() function does not have a None value.

    If you are still getting this error in your Python program, please share your code and query in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments