Last updated: Apr 8, 2024
Reading timeยท5 min
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.
Here is an example of how the error occurs.
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.
split()
method is string-specific, so we have to call it on a string, and not on a list object.The str.split()
method can only be called on strings.
You can use the isinstance()
function to check if the variable stores a string
before calling split()
.
my_str = 'bobby,hadz,com' if isinstance(my_str, str): new_list = my_str.split(',') print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com'] else: print('The variable is not a string') print(type(my_str))
If your variable stores a list, you can:
split()
split()
method on each stringOne way to solve the error is to access the list at a specific index before
calling split()
.
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.
We split the string on each hyphen -
in the example, but you can use any other
delimiter.
0
, and the last item has an index of -1
or len(a_list) - 1
.The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
str.split()
method, it splits the input string on one or more whitespace characters.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.
my_list = ['a-b', 'c-d'] new_list = [] for word in my_list: new_list.append(word.split('-')) result = word.split('-') print(result) # ๐๏ธ ['a', 'b'] ['c', 'd'] print(new_list) # ๐๏ธ [['a', 'b'], ['c', 'd']]
We used a for loop to iterate over the list
and called the split()
method to split each string.
Alternatively, you can use a list comprehension.
Here is an example that splits each element in a list and keeps the first part.
my_list = ['a-b', 'c-d'] # โ Split each element in the list and keep the first part result_1 = [item.split('-', 1)[0] for item in my_list] print(result_1) # ๐๏ธ ['a', 'c']
Here is an example that splits each element in a list into nested lists.
my_list = ['a-b', 'c-d'] # โ Split each element in the list into nested lists result_2 = [item.split('-') for item in my_list] print(result_2) # ๐ [['a', 'b'], ['c', 'd']]
Here is an example that splits each element in a list and flattens the list.
my_list = ['a-b', 'c-d'] # โ Split each element in the list and flatten the list result_3 = [item.split('-') for item in my_list] result_3_flat = [item for l in result_3 for item in l] print(result_3_flat) # ๐๏ธ ['a', 'b', 'c', 'd']
Here is the example.txt
text file that is used in the code samples below.
name,age,country Alice,30,Austria Bobby,35,Belgium Carl,40,Canada
If you are reading from a file and need to split each line on the file, use a
for
loop.
# ['name', 'age', 'country\n'] # ['Alice', '30', 'Austria\n'] # ['Bobby', '35', 'Belgium\n'] # ['Carl', '40', 'Canada'] with open('example.txt', 'r', encoding="utf-8") as f: for line in f: # ๐๏ธ Split the line on each comma new_list = line.split(',') print(new_list)
We opened a file called example.txt
and used a for
loop to iterate over the
lines in the file.
split()
method to split the line on each occurrence of a comma.If you need to read a file line by line, use the following code sample instead.
with open('example.txt', 'r', encoding="utf-8") as f: for line in f: # name,age,country # Alice,30,Austria # Bobby,35,Belgium # Carl,40,Canada print(line.rstrip())
The code sample iterates over the file, reads each line and uses the str.rstrip() method to remove the trailing newline characters.
If you only need to print the Nth element of each line, use the split()
method
and access the result at an index.
with open('example.txt', 'r', encoding="utf-8") as f: for line in f: new_list = line.split(',') # name # Alice # Bobby # Carl print(new_list[0])
We split each line in the list and accessed the new list at index 0
to only
get the first column.
If you need to split the characters of a list element without a separator, use a list comprehension.
my_list = ['hello'] result = [char for char in my_list[0]] print(result) # ๐๏ธ ['h', 'e', 'l', 'l', 'o']
The error 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
.
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.
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 will get the "AttributeError: list object has no attribute" error.
Since split()
is not a method implemented by lists, the error is caused.