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

[ad_1]

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.

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.

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.



[ad_2]

Source link