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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "TypeError: object of type 'NoneType' has no len()" occurs when we pass a None value to the len() function. To solve the error, figure out where the variable got assigned a None value and correct the assignment.

typeerror object of type nonetype has no len

Here is an example of how the error occurs.

main.py
my_list = None # ⛔️ TypeError: object of type 'NoneType' has no len() print(len(my_list))

We tried getting the length of a None value which causes the error.

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function (e.g. sort()) that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

Functions that don't explicitly return a value return None.

main.py
# 👇️ this function returns None def get_list(): print(['a', 'b', 'c']) # 👇️ None my_list = get_list() # ⛔️ TypeError: object of type 'NoneType' has no len() print(len(my_list))

You can use the return statement to return a value from a function.

main.py
def get_list(): return ['a', 'b', 'c'] # 👇️ None my_list = get_list() print(len(my_list)) # 👉️ 3

Use an if statement if you need to check whether a variable doesn't store a None value before getting its length.

main.py
my_list = None if my_list is not None: print(len(my_list)) else: # 👇️ this runs print('Variable stores a None value')

Alternatively, you can provide an empty list or string as a fallback.

main.py
my_list = None if my_list is None: my_list = [] print(len(my_list)) # 👉️ 0
Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.
main.py
my_list = ['c', 'b', 'a'] # 👇️ None result = my_list.sort() print(result) # 👉️ None print(my_list) # 👉️ ['a', 'b', 'c']

Make sure you aren't storing the result of calling one such function in a variable.

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
def get_list(a): if len(a) > 3: return a # 👇️ None my_list = get_list(['a', 'b']) # ⛔️ TypeError: object of type 'NoneType' has no len() print(len(my_list))

The if statement in the get_list function is only run if the passed in argument has a length greater than 3.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error in this scenario, you either have to check if the function didn't return None, or return a default value if the condition is not met.

main.py
def get_list(a): if len(a) > 3: return a return [] # 👈️ return empty list if condition not met my_list = get_list(['a', 'b']) print(my_list) # 👉️ [] print(len(my_list)) # 👉️ 0

Now the function is guaranteed to return a value regardless if the condition is met.

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

When we pass an object to the len() function, Python calls the __len__() method of the object under the hood.

Since None values don't have a __len__ attribute, the error is caused.

To solve the error, track down where the variable got assigned a None value and correct the assignment.

Conclusion #

The Python "TypeError: object of type 'NoneType' has no len()" occurs when we pass a None value to the len() function. To solve the error, figure out where the variable got assigned a None value and correct the assignment.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.