Last updated: Apr 8, 2024
Reading timeยท4 min
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.
Here is an example of how the error occurs.
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.
None
before using len()
Use an if
statement if you need to
check if a variable doesn't store a None value
before getting its length.
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 an empty string as a fallback.
my_list = None if my_list is None: my_list = [] print(len(my_list)) # ๐๏ธ 0
If the variable stores a None
value, it gets set to an empty list.
You can also add an else
statement.
my_list = None if my_list is None: my_list = [] else: print('The variable is not None', len(my_list)) print(len(my_list)) # ๐๏ธ 0
If the variable stores a None
value, it gets set to an empty list in the if
block.
Otherwise, the else
block runs and the variable isn't None
.
None
valuesThe most common sources of None
values are:
None
implicitly).None
.sort()
) that doesn't return anything.None
Functions that don't explicitly return a value return None
.
# ๐๏ธ 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.
def get_list(): return ['a', 'b', 'c'] # ๐๏ธ None my_list = get_list() print(len(my_list)) # ๐๏ธ 3
We used a return
statement to return a list from the function and passed the
list to the len()
function.
None
in PythonNote that there are many built-in functions (e.g. sort()
) that mutate the
original object in place and return None
.
my_list = ['c', 'b', 'a'] # ๐๏ธ None result = my_list.sort() print(result) # ๐๏ธ None print(my_list) # ๐๏ธ ['a', 'b', 'c']
There is a convention in Python for built-in methods that mutate an object in place to return None.
Methods like list.append()
, list.extend()
, list.sort()
mutate the list in
place and return None.
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.
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
.
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.
def get_list(a): if len(a) > 3: return a return [] # ๐๏ธ Return an empty list if the condition is 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 of whether the condition is met.
The len() function returns the length (the number of items) of an object.
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.
You can learn more about the related topics by checking out the following tutorials: