How to Solve Python AttributeError: ‘list’ object has no attribute ‘len’

by | Programming, Python, Tips

This error occurs when you try to call len() on a list object. len() is a built-in function, which returns the length of an iterable. You can solve this error by passing the list to the len() function to get the list. For example,

my_lst = [2, 4, 6, 8, 10]

length = len(my_lst)

print(f'Length of list is {length}')

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


AttributeError: ‘list’ object has no attribute ‘len’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘list’ object has no attribute ‘len’” tells us that the list object we handle does not have the attribute len. The len function is built-in to Python and returns the length (number of items) of an object.

The len() function implicitly calls an object’s __len__ method. A list object has a __len__ method but it is not usual to call it directly. We can verify if an attribute exists for an object using the dir() function. For example,

my_lst = [1, 3, 5, 7, 9]

print('__len__' in dir(my_lst))

print(my_lst.__len__())
True

5

Note that when we call the __len__() method on the list object, we get the list of the object. But the typical approach for getting the length of an object is to pass it to the built-in len() function.

Example

Let’s look at an example of trying to call the len() method on a list.

# Create list

lst = [2, 10, 11, 2, 4, 5]

# Attempt to get length of list

print(lst.len())

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 3>()
      1 lst = [2, 10, 11, 2, 4, 5]
----> 3 print(lst.len())

AttributeError: 'list' object has no attribute 'len'

The error occurs because len() is not a method of the list object.

# Get list of attributes for list object

print(dir(lst))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Passing the list object to the built-in function returns the list of attributes for the object. We can see that len() is not in the list of attributes.

Solution

We can solve the error by passing the list object to the built-in len() function. Let’s look at the revised code:

lst = [2, 10, 11, 2, 4, 5]

length = len(lst)

print(f'Length of list is: {length}')

Let’s run the code to get the result:

Length of list is: 6

We successfully retrieved the length of the list.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on AttributeErrors involving the list object, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!