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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'list' object has no attribute 'split' #

The Python "AttributeError: 'list' object has no attribute 'split'" occurs when we call the split() method on a list instead of a string. To solve the error, call split() on a string, e.g. by accessing the list at a specific index or by iterating over the list.

attributeerror list object has no attribute split

Here is an example of how the error occurs.

main.py
my_list = ['a-b', 'c-d'] # ⛔️ AttributeError: 'list' object has no attribute 'split' print(my_list.split('-'))

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

The split() method is string-specific, so we have to call it on a string, and not on a list object.

One way to solve the error is to access the list at a specific index before calling split().

main.py
my_list = ['a-b', 'c-d'] result = my_list[0].split('-') print(result) # 👉️ ['a', 'b']

We accessed the list element at index 0 and called the split() method on the string.

The split() method splits the string on each occurrence of the provided separator into a list of substrings.

If you need to call the split() method on each string in a list, use a for loop to iterate over the list.

main.py
my_list = ['a-b', 'c-d'] for word in my_list: result = word.split('-') print(result) # 👉️ ['a', 'b'] ['c', 'd']

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

If you are reading from a file and need to split each line on the file, use a for loop.

main.py
with open('example.txt', 'r', encoding="utf-8") as f: for line in f: # 👇️ split line on each comma result = line.split(',') print(result)

We opened a file called example.txt and used a for loop to iterate over the lines in the file.

On each iteration, we used the split() method to split the line on each occurrence of a comma.

If you need to split the characters of a list element without a separator, use a list comprehension.

main.py
my_list = ['hello'] result = [char for char in my_list[0]] print(result) # 👉️ ['h', 'e', 'l', 'l', 'o']
The "AttributeError: 'list' object has no attribute 'split'" occurs when we try to call the split() method on a list instead of a string.

To solve the error, you either have to correct the assignment of the variable and make sure to call split() on a string, or call split() on an element in the list that is of type string.

You can either access the list at a specific index, e.g. my_list[0] or use a for loop to iterate over the list if you have to call split() on each element.

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.

Since split() is not a method implemented by lists, the error is caused.

Conclusion #

The Python "AttributeError: 'list' object has no attribute 'split'" occurs when we call the split() method on a list instead of a string. To solve the error, call split() on a string, e.g. by accessing the list at a specific index or by iterating over the list.

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.