Python AttributeError: 'NoneType' object has no attribute 'append' Solution

Posted in /  

Python AttributeError: 'NoneType' object has no attribute 'append' Solution
vinaykhatri

Vinay Khatri
Last updated on October 6, 2024

    The Python append() is a list method to add a new element object at the end of the list. But if we use a append() method on a NoneType object, we encounter the AttributeError: 'NoneType' object has no attribute 'append' .

    In this Python tutorial, we will explore this error and learn why it occurs and how to solve it. To understand the error better, we will discuss a typical scenario where most Python learners encounter this error.

    So, let's get started!

    Python Error - AttributeError: 'NoneType' object has no attribute 'append'

    NoneType is a type for the None object, which has no value. Functions that don't return anything has the return value 'None'. When we use the append() method on the NoneType object, we receive the error AttributeError: 'NoneType' object has no attribute 'append' .

    The error has two parts -

    1. Exception Type ( AttributeError )
    2. Error Message ( 'NoneType' object has no attribute 'append' )

    1. Exception Type ( AttributeError )

    AttributeError is one of the standard Python exceptions. It occurs when we try to access an unsupported attribute (property or method) using an object.

    For example, the append() method is exclusive to Python lists. But if we try to apply it to a tuple object, we receive the AttributeError. This is because tuple objects do not have the append() method.

    tuple_ = (1,2,3,4,5)
    tuple_.append(6)  #error

    Output

    Traceback (most recent call last):
      File "/home/main.py", line 2, in <module>
        tuple_.append(6)  #error
    AttributeError: 'tuple' object has no attribute 'append'

    2. Error Message ( 'NoneType' object has no attribute 'append' )

    The error message 'NoneType' object has no attribute 'append' " tells us that the append() method is not supported on the NoneType object . This means we called the append() method on a variable whose value is None .

    Example

    # A None value object
    a = None
    
    # calling append() method on the None value
    a.append(2)
    
    print(a)

    Output

    Traceback (most recent call last):
    File "main.py", line 5, in <module>
    a.append(2)
    AttributeError: 'NoneType' object has no attribute 'append'

    Break the code

    In the above example, we received the error at line 5 with the a.append(2) statement. The value of a is None , and None value does not have any append() method. Hence, we receive this error.

    Causes of AttributeError: 'NoneType' object has no attribute 'append'

    There are many reasons for this error to occur:

    • Having a function that does not return anything or returns None explicitly.
    • Setting a variable to None explicitly.
    • Assigning a variable to the result of calling a function that does not return anything.
    • Having a function that returns a value only if the condition is met.

    Common Example Scenario

    The most common scenario when novice Python programmers commit this error is when they assign the return value of the append() method to a Python list variable name and try to call the append() method again on the same object. The append() method can only append a new value at the end of the list object, and it does not return any value, which means it returns None .

    For Example

    # list object
    my_list = [1,2,3,4,5]
    
    # return value of append method 
    return_value = my_list.append(6)
    
    print(return_value)

    Output

    None

    From the output, you can see that we get None value when we try to assign the return value of append() method to a variable.

    Many new Python learners do not know about the None return value of the append() method. They assign the append() method calling statement to the list object, which makes the list object value to None . And when they again try to append a new value to the list, they encounter the AttributeError: 'NoneType' object has no attribute 'append' Error.

    For Example

    Let's write a Python program for to-do tasks. The program will ask the user to enter the 5 tasks that he/she wants to perform. We will store all those tasks using a list object, todos . To add the tasks entered by the user, we will use the list append() method.

    # create a empty list
    todos = []
    
    for i in range(1,6):
        task = input(f"Todo {i}: ")
    
        # add the task to the todo list
        todos =  todos.append(task)
    
    print("****Your's Today Tasks******")
    for i in todos:
        print(i)

    Output

    Todo 1: workout
    Todo 2: clean the house
    Traceback (most recent call last):
    File "main.py", line 8, in <module>
    todos = todos.append(task)
    AttributeError: 'NoneType' object has no attribute 'append'

    Break the code

    In the above example, we get the error at line 8 with the statement todos = todos.append(task) . The error occurs during the second iteration of the for loop when we pass the Todo 2: clean the house value as an input.

    In the first iteration, when we pass the Todo 1: workout value, the todos = todos.append(task) statement set the value of todos to None , because the value returned by the todos.append(task) statement is None.

    That's why, in the second iteration, when Python tries to call the append() method on the None object, it threw the AttributeError: 'NoneType' object has no attribute 'append' error.

    Solution

    The solution to the above problem is straightforward. We do not need to assign the return value to any object when we use the append() method on a Python list. The simple call of the append() method on a list object will add the new element to the end of the list.

    To solve the above example, we only need to ensure that we are not assigning the append() method return value our todos list.

    Example Solution

    # create a empty list
    todos = []
    
    for i in range(1,6):
        task = input(f"Todo {i}: ")
    
        # add the task to the todo list
        todos.append(task)
    
    print("****Your's Today Tasks******")
    for i in todos:
        print(i)

    Output

    Todo 1: workout
    Todo 2: clean the house
    Todo 3: have a shower
    Todo 4: make the breakfast
    Todo 5: start coding
    ****Your's Today Tasks******
    workout
    clean the house
    have a shower
    make the breakfast
    start coding

    Final Thoughts!

    This was all about the most common Python error AttributeError: 'NoneType' object has no attribute 'append' . The error occurs when we try to call the append() method on a None value. To resolve this error, we need to ensure that we do not assign any None or return value of the append() method to a list object.

    If you still get this error in your Python program, you can share your code in the Comment section. We will try to help you with debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments