The len()
method only works on iterable objects such as strings, lists, and dictionaries. This is because iterable objects contain sequences of values. If you try to use the len()
method on a None value, you’ll encounter the error “TypeError: object of type ‘NoneType’ has no len()
”.
In this guide, we talk about what this error means and how it works. We walk through two examples of this error in action so you can figure out how to solve it in your code.
TypeError: object of type ‘NoneType’ has no len()
NoneType refers to the None data type. You cannot use methods that would work on iterable objects, such as len()
, on a None value. This is because None does not contain a collection of values. The length of None cannot be calculated because None has no child values.
This error is common in two cases:
- Where you forget that built-in functions change a list in-place
- Where you forget a return statement in a function
Let’s take a look at each of these causes in depth.
Cause #1: Built-In Functions Change Lists In-Place
We’re going to build a program that sorts a list of dictionaries containing information about students at a school. We’ll sort this list in ascending order of a student’s grade on their last test.
To start, define a list of dictionaries that contains information about students and their most recent test score:
students = [ {"name": "Peter", "score": 76 }, {"name": "Richard", "score": 63 }, {"name": "Erin", "score": 64 }, {"name": "Miley", "score": 89 } ]
Each dictionary contains two keys and values. One corresponds with the name of a student and the other corresponds with the score a student earned on their last test. Next, use the sort()
method to sort our list of students:
def score_sort(s): return s["score"] sorted_students = students.sort(key=score_sort)
We have declared a function called “score_sort” which returns the value of “score” in each dictionary. We then use this to order the items in our list of dictionaries using the sort()
method.
Next, we print out the length of our list:
print("There are {} students in the list.".format(len(sorted_students)))
We print out the new list of dictionaries to the console using a for loop:
for s in sorted_students: print("{} earned a score of {} on their last test.".format(s["name"], s["score"]))
This code prints out a message informing us of how many marks a student earned on their last test for each student in the “sorted_students” list. Let’s run our code:
Traceback (most recent call last): File "main.py", line 13, in <module> print("There are {} students in the list.".format(len(sorted_students))) TypeError: object of type 'NoneType' has no len()
Our code returns an error.
To solve this problem, we need to remove the code where we assign the result of the sort()
method to “sorted_students”. This is because the sort()
method changes a list in place. It does not create a new list.
Remove the declaration of the “sorted_students” list and use “students” in the rest of our program:
students.sort(key=score_sort) print("There are {} students in the list.".format(len(students))) for s in students: print("{} earned a score of {} on their last test.".format(s["name"], s["score"]))
Run our code and see what happens:
There are 4 students in the list. Richard earned a score of 63 on their last test. Erin earned a score of 64 on their last test. Peter earned a score of 76 on their last test. Miley earned a score of 89 on their last test.
Our code executes successfully. First, our code tells us how many students are in our list. Our code then prints out information about each student and how many marks they earned on their last test. This information is printed out in ascending order of a student’s grade.
Cause #2: Forgetting a Return Statement
We’re going to make our code more modular. To do this, we move our sorting method into its own function. We’ll also define a function that prints out information about what score each student earned on their test.
Start by defining our list of students and our sorting helper function. We’ll borrow this code from earlier in the tutorial.
students = [ {"name": "Peter", "score": 76 }, {"name": "Richard", "score": 63 }, {"name": "Erin", "score": 64 }, {"name": "Miley", "score": 89 } ] def score_sort(s): return s["score"]
Next, write a function that sorts our list:
def sort_list(students): students.sort(key=score_sort)
Finally, we define a function that displays information about each students’ performance:
def show_students(new_students): print("There are {} students in the list.".format(len(students))) for s in new_students: print("{} earned a score of {} on their last test.".format(s["name"], s["score"]))
Before we run our code, we have to call our functions:
new_students = sort_list(students) show_students(new_students)
Our program will first sort our list using the sort_list()
function. Then, our program will print out information about each student to the console. This is handled in the show_students()
function.
Let’s run our code:
Traceback (most recent call last): File "main.py", line 21, in <module> show_students(new_students) File "main.py", line 15, in show_students print("There are {} students in the list.".format(len(new_students))) TypeError: object of type 'NoneType' has no len()
Our code returns an error. This error has occured because we have forgotten to include a “return” statement in our “sort_list” function.
When we call our sort_list()
function, we assign its response to the variable “new_students”. That variable is passed into our show_students()
function that displays information about each student. To solve this error, we must add a return statement to the sort_list()
function:
def sort_list(students): students.sort(key=score_sort) return students
Run our code:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
There are 4 students in the list. Richard earned a score of 63 on their last test. Erin earned a score of 64 on their last test. Peter earned a score of 76 on their last test. Miley earned a score of 89 on their last test.
Our code returns the response we expected.
Conclusion
The “TypeError: object of type ‘NoneType’ has no len()
” error is caused when you try to use the len()
method on an object whose value is None.
To solve this error, make sure that you are not assigning the responses of any built-in list methods, like sort()
, to a variable. If this does not solve the error, make sure your program has all the “return” statements it needs to function successfully.
Now you’re ready to solve this problem like a Python professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.