Only iterable objects such as strings and lists have a length. If you try to calculate the length of a number, you’ll encounter the “TypeError: object of type ‘int’ has no len()” error.
In this guide, we discuss what this error means and why it is raised. We’ll explore an example of a code snippet with this error so we can walk through how to fix the issue.
TypeError: object of type ‘int’ has no len()
The len() method returns the length of an object. Consider the following program:
browsers = ["Microsoft Edge", "Mozilla Firefox", "Google Chrome") print(len(browsers))
Our program returns “3”. This is the length of our Python list. The length of the “browsers” object can be calculated because it contains multiple values.
Strings, tuples, and lists all work with the len()
method because they are sequences of values. Integer values do not have a length because they only represent one value. Integers store a number which does not have a “length” like any other iterable object.
An Example Scenario
We’re going to write a program that calculates how many customers at an electronics store are part of a loyalty club program. We’ll start with a list of dictionaries which each store information about our customers:
customers = [ { "name": "Lesley Jones", "loyalty": True }, { "name": "Benjamin Logan", "loyalty": True }, { "name": "Hannah Mason", "loyalty": False } ]
Our customers list contains three dictionaries. Next, use a for loop to calculate how many customers are loyalty club members:
number_of_loyalty = 0 for c in customers: if c["loyalty"] == True: number_of_loyalty += 1
We have defined a variable called “number_of_loyalty”. We use this to keep track of how many customers are in the loyalty club.
We calculate whether a customer is a loyalty club member by evaluating whether the value of “loyalty” in each dictionary is equal to True.
Next, print a message to the console that tells us how many customers are loyalty members:
print("Out of the {} customers at the store, {} of them are loyalty club members.".format(len(customers), len(number_of_loyalty)))
The .format() statement lets us add values into our string at specific positions. In this case, we are adding in two values to our string: the number of customers evaluated, and how many customers are in the loyalty club.
Let’s run our code to see if it works:
Traceback (most recent call last): File "main.py", line 13, in <module> print("Out of the {} customers at the store, {} of them are loyalty club members.".format(len(customers), len(number_of_loyalty))) TypeError: object of type 'int' has no len()
Our code stops working on line 13. This is the last line of our program.
The Solution
The error message tells us we have tried to use a method on an object that does not support that method. We’ve tried to calculate the length of an integer value.
In our print statement, we ask our program to evaluate the length of the “number_of_loyalty” value:
len(number_of_loyalty)
This is unnecessary because “number_of_loyalty” itself tracks how many loyalty club members there are. To fix this error, we can remove the len()
from around the “number_of_loyalty” value so we can see the integer value it stores:
print("Out of the {} customers at the store, {} of them are loyalty club members.".format(len(customers), number_of_loyalty))
Let’s run our revised program:
Out of the 3 customers at the store, 2 of them are loyalty club members.
Our program calculates that two out of the three customers at the store are loyalty club members. This was the output we expected. If we look at our initial list, we can see that both “Lesley Jones” and “Benjamin Logan” are loyalty card members.
Conclusion
The “TypeError: object of type ‘int’ has no len()” error is raised when you try to find the length of an integer value. To fix this issue, remove the len() method from around any integer value in your program.
Now you have the knowledge you need to fix this common Python error like a pro!
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.