AttributeError: 'list' object has no attribute 'X' in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'list' object has no attribute 'X' in Python #

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.

attributeerror list object has no attribute

Here is an example of how the error occurs.

main.py
my_list = ['a_b', 'c_d'] # 👈️ list # ⛔️ AttributeError: 'list' object has no attribute 'split' result = my_list.split('_')

We created a list with 2 elements and tried to call the split() method on the list which caused the error.

To solve the error in this situation, we have to access the list at a specific index because split is a method on strings.

main.py
my_list = ['a_b', 'c_d'] result = my_list[0].split('_') print(result) # 👉️ ['a', 'b']

We accessed the list at index 0 to called the split() method on the first list element.

If you are trying to call a method on each element in a list, use a for loop to iterate over the list.

main.py
my_list = ['hello', 'world'] for word in my_list: result = word.upper() print(result) # 👉️ "HELLO", "WORLD"

We used a for loop to iterate over the list and called the upper() method to uppercase each string.

If you need to call a function on each element in a list, you can also use a list comprehension.

main.py
my_list = ['hello', 'world'] new_list = [item.upper() for item in my_list] print(new_list) # 👉️ ['HELLO', 'WORLD']

If you meant to join a list into a string with a separator, call the join() method on the string separator instead.

main.py
my_list = '_'.join(['a', 'b', 'c']) print(my_list) # 👉️ 'a_b_c' without_separator = ''.join(['a', 'b', 'c']) print(without_separator) # 👉️ 'abc'

If you created a list by mistake, track down where the variable gets assigned a list and correct the assignment.

The "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on the list data type.

If you meant to use a dictionary, wrap key-value pairs in curly braces.

main.py
my_dict = {'name': 'Alice', 'age': 30} print(my_dict['name']) # 👉️ "Alice" print(my_dict['age']) # 👉️ 30

If you meant to create a class and access its attributes, declare a class and instantiate it.

main.py
class Employee(): def __init__(self, name, age): self.name = name self.age = age # 👇️ create instance emp = Employee('Alice', 30) # 👇️ access attributes print(emp.name) # 👉️ "Alice" print(emp.age) # 👉️ 30

You can view all the attributes an object has by using the dir() function.

main.py
my_list = ['a', 'b', 'c'] # 👉️ [... 'append', 'clear', 'copy', 'count', 'extend', 'index', # 'insert', 'pop', 'remove', 'reverse', 'sort' ...] print(dir(my_list))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: list object has no attribute" error.

Conclusion #

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.