How to Solve Python TypeError: object of type ‘int’ has no len()

by | Programming, Python, Tips

This error occurs when you pass an integer to a len() function call. Integers are whole numbers without decimals. In Python, numerical values do not have a length.

You can solve the error by only passing iterable objects to the len() function. For example, you can pass an integer to a range() function call to get a range object, which is iterable and has a length. For example,

my_int = 5

rng = range(my_int)

print(len(rng))

This tutorial will go through the error and how to solve it with code examples.


TypeError: object of type ‘int’ has no len()

We raise a Python TypeError when attempting to perform an illegal operation for a specific type. In this case, the type is int.

The part ‘has no len()‘ tells us the map object does not have a length, and therefore len() is an illegal operation for the int object.

Retrieving the length of an object is only suitable for iterable objects, like a list or a tuple.

An integer is a whole number, that can be positive or negative, without decimals and of unlimited length.

The len() method implicitly calls the dunder method __len__() which returns a positive integer representing the length of the object on which it is called. All iterable objects have __len__ as an attribute. Let’s check if __len__ is in the list of attributes for an int object and a list object using the built-in dir() method.

my_int = 4

print(type(my_int))

print('__len__' in dir(my_int))
<class 'int'>
False

We can see that __len__ is not present in the attributes of the int object.

lst = ["Einstein", "Feynman", "Dirac"]

print(type(lst))

print('__len__' in dir(lst))
<class 'list'>
True

We can see that __len__ is present in the attributes of the list object.

Example

Let’s look at an example of trying to get the length of an int object. First, we will define a function that counts the number of cakes in a list of baked goods provided by a bakery.

def get_total_cakes(bakery_list):

    cake_count = sum(map(lambda x: "cake" in x, bakery_list))

    print(f'Number of cakes in shop: {len(cake_count)}')

Next, we will define the list of baked goods to pass to the function call.

baked_goods = ['chocolate cake', 'baklava', 'genoise cake', 'chiffon cake', 'brownie', 'strawberry crumble', 'angel food cake']

Next, we will call the get_total_cakes() function to get the number of cakes in the list:

get_total_cakes(baked_goods)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [15], in <cell line: 3>()
      1 baked_goods = ['chocolate cake', 'baklava', 'genoise cake', 'chiffon cake', 'brownie', 'strawberry crumble', 'angel food cake']
----> 3 get_total_cakes(baked_goods)

Input In [14], in get_total_cakes(bakery_list)
      1 def get_total_cakes(bakery_list):
      2     cake_count = sum(map(lambda x: "cake" in x, bakery_list))
----> 3     print(f'Number of cakes in shop: {len(cake_count)}')

TypeError: object of type 'int' has no len()

The error occurs because the cake_count variable is an integer. The combination of sum() and map() counts the elements in a list by a specified condition and returns an integer. Therefore, when we try to get the length of cake_count using len(), we are trying to get the length of an integer.

We can check the type of an object by using the built-in type() function:

baked_goods = ['chocolate cake', 'baklava', 'genoise cake', 'chiffon cake', 'brownie', 'strawberry crumble', 'angel food cake']

cake_count = sum(map(lambda x: "cake" in x, baked_goods))

print(type(cake_count))
<class 'int'>

Solution

We can solve this error by removing the len() function call in the print statement. Let’s look at the revised code:

def get_total_cakes(bakery_list):
    cake_count = sum(map(lambda x: "cake" in x, bakery_list))
    print(f'Number of cakes in shop: {cake_count}')

Let’s define the list of baked goods and pass the list to the get_total_cakes() function call.

baked_goods = ['chocolate cake', 'baklava', 'genoise cake', 'chiffon cake', 'brownie', 'strawberry crumble', 'angel food cake']

get_total_cakes(baked_goods)

Let’s run the code to get the number of cakes.

Number of cakes in shop: 4

Summary

Congratulations on reading to the end of this tutorial!

For further reading on the has no len() TypeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python, which provides the best, easy-to-use online courses.

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨