Python lists cannot be divided into separate lists based on characters that appear in the values of a list. This is unlike strings which values can be separated into a list.
If you try to use the split() method on a list, you get the error “attributeerror: ‘list’ object has no attribute ‘split’”.
In this guide, we talk about what this error means and why you may find it in your code. We also walk through an example scenario to help you figure out how to solve this error.
attributeerror: ‘list’ object has no attribute ‘split’
This error tells us we are trying to use a function that is not available on lists.
The split()
method splits a string into a list. The string is broken up at every point where a separator character appears. For instance, you can divide a string into a list which contains all values that appear after a comma and a space (“, ”):
cakes = "Cheese Scone, Cherry Scone, Fruit Scone" cake_list = cakes.split(", ") print(cake_list)
Our code splits the “cakes” string between the places where a comma followed by a space is present. These values are then added to the list called “cake_list”. Our code returns:
['Cheese Scone', 'Cherry Scone', 'Fruit Scone']
The split()
operation only works on strings.
An Example Scenario
We have a CSV file which contains information about cakes sold at a tea house. We want to print out the name of each cake to the Python shell so that customers can choose what they want to have with their drink.
Our CSV file looks like this:
Cheese Scone, $1.30, Vegetarian Toasted Teacake, $1.50, Vegetarian Fruit Bread, $1.40, Vegetarian
Our file contains three entries: one for cheese scones, one for toasted teacakes, and one for fruit bread. We read this file into our program so that we an access our values:
with open("cakes.csv", "r") as file: cakes = file.readlines() cake_names = cakes.split(", ")[0] print(cake_names)
This program reads the “cakes.csv” file. It then uses the split()
method to split up the values in each record so that we can access the names of each cake.
We use the [0] indexing syntax to access the first item in a record. This corresponds to the name of a cake.
Let’s run our code and see what happens:
Traceback (most recent call last): File "main.py", line 4, in <module> cake_names = cakes.split(", ")[0] AttributeError: 'list' object has no attribute 'split'
Our code, as expected, returns an error.
The Solution
We try to use the split()
method on a list. Let’s print out the contents of “cakes” to the console:
with open("cakes.csv", "r") as file: cakes = file.readlines() print(cakes)
Our code returns:
['Cheese Scone, $1.30, Vegetarian\n', 'Toasted Teacake, $1.50, Vegetarian\n', 'Fruit Bread, $1.40, Vegetarian\n']
Our code cannot separate a list into multiple lists using split()
. This is because lists are already separated by commas. Instead, we should use the split()
method on each item in our list.
We can do this by using a for loop to iterate over every line in the “cakes.csv” file:
with open("cakes.csv", "r") as file: cakes = file.readlines() for c in cakes: split_lines = c.split(", ") print(split_lines[0])
We initialized a for loop that goes through every line in the “cakes” variable. We use the split()
method to divide each string value in the list by the “, ”string pattern. This means the cake names, prices, and vegetarian status are to be divided into a list.
On the last line of our code, we use split_lines[0]
to print out the first item in each new list. This is equal to the name of each cake. Let’s try to run our code:
Cheese Scone Toasted Teacake Fruit Bread
Our code successfully prints out a list of cakes. This is because we did not separate a list. We use split()
to separate all the items in each string that appears in our list.
Conclusion
The “attributeerror: ‘list’ object has no attribute ‘split’” error is raised when you try to divide a list into multiple lists using the split()
method.
You solve this error by ensuring you only use split()
on a string. If you read a file into a program, make sure you use split()
on each individual line in the file, rather than a list of all the lines.
Now you’re ready to solve this common Python error like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.