AttributeError: ‘int’ object has no attribute ‘append’ ( Solved )

AttributeError_ 'int' object has no attribute 'append'

There are many inbuilt functions in the Python programming language for the data structure. The function append() is one of them. It allows you to add elements at the end of the list. But while implementing this method, make sure to use it correctly otherwise you can get AttributeError: ‘int’ object has no attribute ‘append’ error.

In this entire tutorial, you will know why this error comes and how to solve it.

What is AttributeError ?

Before knowing the cause and solution of ‘int’ object has no attribute ‘append’ error lets know what is attributeerror. The AttributeError is an error that you mostly gets when you are using a function in the code that is not defined by the object. For example if you are getting the AttributeError: ‘X’ object has no attribute ‘Y’  then it measn the the module or object X has not function Y defined or the function Y must be defined in the the other object or module.

Cause of AttributeError: ‘int’ object has no attribute ‘append’ Error

The main cause of the ‘int’ object has no attribute ‘append’ is that you are appending new elements in the integer part. Let’s say I have a list of integers. If I will try to add a new integer to the selected element from the list, then I will get the error.

sample_list = [10,20,30,40,50]
sample_list[1].append(60)

Output

int object has not attribute append
int object has not attribute append

The other case when you will get the error is applying append() method to the integer variable by mistake. For example, I have a variable of integer type. If I append another integer to this integer variable then I will get the ‘int’ object has no attribute ‘append’ error.

value = 10
value.append(20)

Output

int object has not attribute append for integer variable
int object has not attribute append for integer variable

Solution of AttributeError: ‘int’ object has no attribute ‘append’

The solution to this attributeError is very simple. Look at the causes of this error. The error was mostly due to appending elements or using the append() method on the variable of integer type.

So to not get this AttributeError you have to use the append() method on the list type variable only.

Now if you run the below lines of code then you will not get the AttributeError: ‘int’ object has no attribute ‘append’.

sample_list = [10,20,30,40,50]
sample_list.append(60)
print(sample_list)

Output

Appending new element to the existing list
Appending a new element to the existing list

Conclusion

Sometimes you have to carefully use the python inbuilt function. You should be aware that what is the type of the variable is accepted by any method. The above solution work best for the  AttributeError: ‘int’ object has no attribute ‘append’ error.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help. Here are some similar errors.

1.Attributeerror: dict object has no attribute append ( Solved )

2.AttributeError: str object has no attribute append (Solved )

What is AttributeError in Python ? Complete Overview

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner