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

Posted in /  

Python AttributeError: 'list' object has no attribute 'split' Solution
vinaykhatri

Vinay Khatri
Last updated on March 29, 2024

    Python list is a built-in data structure that stores its elements in sequential order. And if we wish to convert a Python string to a list object, we can apply the spilt() method on the string and convert it into a list of strings. But if we try to call the split() method  on a list, we will receive the Error AttributeError: 'list' object has no attribute 'split' .

    In this Python guide, we will discuss this error in detail and learn how to solve it. We will also demonstrate an example so you can have a better understanding of this error.

    So, let's get started with the error

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

    Let's break down the error statement " AttributeError: 'list' object has no attribute 'split' " into two parts

    1. AttributeError (Exception Type)
    2. 'list' object has no attribute split (Error Message)

    1. AttributeError

    AttributeError is one of the standard Python exceptions. It occurs in a Python program when we try to access an undefined attribute on an object.

    2. 'list' object has no attribute split

    This is the error message specifying that the list object has no attribute (method or property) by name split. This error message only occurs in a Python program when we call the split() method or split property on a list object or variable. split() is a string method that can convert a string value to a list by separating the string based on the separator passed in the split() method.

    Example

    # string
    sentence = "Hello World How are you doing"
    
    # convert the string into a list
    words = sentence.split()
    
    print(words)

    Output

    ['Hello', 'World', 'How', 'are', 'you', 'doing']
    

    Unlike string, the list does not support the split() method, and when we try to call a split() method on a list, we receive the AttributeError with 'list' object has no attribute split Error Message. To know more about the split() method, click here .

    Error

    # list
    sentences = ["Hello World How are you doing", "Doing great"]
    
    # perfom split method on a list
    sentences.split()

    Output

    Traceback (most recent call last):
        File "main.py", line 5, in <module>
            sentences.split()
    AttributeError: 'list' object has no attribute 'split'

    Common Example Scenario

    You will only get this error in your Python program when you apply the split attribute(property or method) on a list object. Many Python learners who are new to programming do not have a complete idea about the return values, and sometimes they simply apply the method to the wrong data type. Let's understand this error with an example

    Example

    For instance, we have a list of students' detail, and the detail is in the form of a string in such a format "fname-lname-age-class" . And we need a filter that lists string data further so we can print and show the data in a more readable format.

    # list object
    students = [
                'Rahul-Jain-17-12th',
                'Ravi-Sharma-17-12th',
                'Jiya-Rana-16-12th',
                'Rohan-Sina-18-12th',
                'Himanshu-Kumar-17-12th'
                ]
    
    print("Name  \t\t Age  \t Class\n------------------------------ ")
    # show the students data
    for student in students:
        # error applying split on the students (list)
        student = students.split('-')
    
        print(f"{student[0]} {student[1]} \t {student[2]} \t {student[3]}")

    Output

    Traceback (most recent call last):
      File "main.py", line 14, in 
        student = students.split('-')
    AttributeError: 'list' object has no attribute 'split'

    Break the code

    In this example, we are getting this error in line 14 with student = students.split('-') statement. This is because the students is a list name, and we are supposed to perform the split('-') method on the student name, which is the string element value we are getting with each iteration from students.

    Solution

    To solve the above program, all we need to do is replace the students.split('-') statement with student.split('-') statement. So the split() method could work on the string value.

    Example Solution

    # list object
    students = [
                'Rahul-Jain-17-12th',
                'Ravi-Sharma-17-12th',
                'Jiya-Rana-16-12th',
                'Rohan-Sina-18-12th',
                'Himanshu-Kumar-17-12th'
                ]
    
    print("Name  \t\t Age  \t Class\n------------------------------ ")
    # show the students data
    for student in students:
        # solve
        student = student.split('-')
    
        print(f"{student[0]} {student[1]} \t {student[2]} \t {student[3]}")

    Output

    Name             Age     Class
    ------------------------------
    Rahul Jain       17      12th
    Ravi Sharma      17      12th
    Jiya Rana        16      12th
    Rohan Sina       18      12th
    Himanshu Kumar   17      12th

    Final Thoughts!

    In this Python error guide, we discussed the " AttributeError: 'list' object has no attribute 'split' " Error. This error is raised in a Python program when we try to call the split() method on a list object or value. The list does not support the split method. It is a string method that converts a string value into a  list by separating the string based on the separator passed in the split() method.

    If you are still getting this error in your program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments