AttributeError: ‘NoneType’ object has no attribute ‘append’

NoneType” in Python is the data type of object having “None” or “Null” value. There are several built-in methods, like “sort()”, append(), etc., that return the “None” value. The “None” value can also be assigned to a variable utilizing the “None” keyword. When we try the “append()” method on none value, then “AttributeError” occurs in the python program. To resolve this error, various solutions are provided in this Python guide. 

This Python write-up will provide you with various reasons and solutions regarding the “AttributeError: NoneType object has no attribute append” in Python. The points given below are demonstrated with examples in this Python blog post:

So, let’s get started!

Reason 1: Calling append() Method on None Value

The “AttributeError: NoneType object has no attribute append” occurs when a user tries to call the append() method on the “None” value. The error is demonstrated in the below snippet:

How to install Kali Linux in Window...

The above output shows that the“AttributeError” occurs when the “append()” method is applied on none value.

Note: The “append()” method is utilized in Python to append the element at the end of the list. The “append()” method updates the existing list, so it does not return any value.

Solution: Using if-else Condition Statement

To solve this error, we need to check the none value of the variable by using the “if-else” statement in a program.

Code:

list_value = None

if list_value is not None:
    print('variable is NOT None')
    list_value.append('Lily')
else:
    print('variable is None')

In the above code:

  • The “if-else” condition is used with the combination of “is not” operator to check whether the input variable is “none” or not.
  • If the input variable is not equal to none, then the statements specified within the “if” block will be executed, and the value of the list will be appended using the “append()” function.
  • If the variable value is none, then the “else” block will be executed, and the “variable is none” message will be printed on the screen using the “print()” function.

Output:

The output proves that the “AttributeError” has been resolved using the if-else statement.

Reason 2: Function Returns None

The “AttributeError: NoneType object has no attribute append” will also occur when a function does not return any value. The following snippet shows AttributeError:

The above output shows an error when the “append()” function is called on none value of the function.

Solution: Return a Value to a Function

To resolve this error, return the value of the function to the caller or main program. In the example code given below, the list value is returned to function using the “return” statement:

Code:

def sample():
    return(['Lily'])

list_value = sample()

list_value.append('John')
print(list_value)

In the above code, the return statement returns the value of the list to the function. The “append()” function appends the new value “John” at the end of the returned list and displays it on the screen using the print() function.

Output:

The above output shows that the element value “John” is appended to a list value returned by the sample function.

Reason 3: Built-in Function Returns None

There is some function in Python that returns a None value, such as sort(), append(), etc. So, the “AttributeError” occurs when the “append()” function is called on the return value of these functions.

The above output shows an AttributeError when we append the element “John” to the output value of the “sort()” function.

Solution: Calling the Append Function on the List Containing Value

To resolve this error, always call the “append()” function on the input function that has some value. In our scenario, the “sort()” method updates the existing list and does not return any value. So to append any value, always call an “append()” function on the original value.

Code:

list_value = ['cyndy', 'blake', 'anna']

print('Before Sorting List: ', list_value)

list_value.sort()

print('\nAfter Sorting List: ', list_value)

list_value.append('John')

print('\nAfter Appending List: ', list_value)

In the above code, everything is applied to the original list value. The “sort()” method sorts the list elements, and its return value will be none. The “append()” function is applied to the original sorted list rather than to the result of the “sort()” method.

Output:

The original list, sorted list, and appended list have been shown in the above output.

That’s it from this guide!

Conclusion

The “AttributeError: NoneType object has no attribute append” occurs when a user tries to call an “append()” function on none value. To resolve this error, various solutions are provided in this Python guide, e.g., check the “None” value before applying the “append()” function, return some value to the function, and always append the function that has some value. This article has presented various reasons, and solutions for the “NoneType object has no attribute append” error in Python using some appropriate examples.