attributeerror: int object has no attribute append

Python is a versatile language that can be used for a variety of purposes from developing small scripts to creating entire applications. One of the advantages of Python is the large number of modules that are available for download from the Python Package Index (PyPI), a repository of software for the Python programming language. However, on occasion, you may encounter an error message indicating that a particular module is not found. In this blog post, we will show how to solve the error message “ attributeerror: int object has no attribute append ” in Python.

 

 

What is an attributeerror?

An attributeerror is a type of error that is raised when an attribute reference (a.b) is invalid. This can happen when a variable is not defined, or when an object is not found. Also, an attributeerror is an error that is caused when an attribute (a piece of information that is associated with an object) is accessed, but the object doesn’t have that attribute.

There are three common causes of attributeerrors:

1. The attribute doesn’t exist.

2. The attribute is spelled incorrectly.

3. The attribute is inaccessible (for example, it’s private).

 

 

 

Why this attributeerror occurs in python

AttributeError: ‘int’ object has no attribute ‘append’” occurs when you call the append() method on an integer object. We can call the append function to the list type object, not on the integer type object.

For Eg.

#no_list is a list type 
no_list = ['1','2','3','4']


#reassign value to no_list is 10, now the no_list is an integer type

no_list = 10

#We can see the type 

print(type(no_list))


#now we will try to append value 5 in the list, we will be getting error 

no_list.append('5')

Output:

attributeerror: int object has no attribute append

 

 

 

 

How to fix attributeerror: int object has no attribute append in python?

For solving this error just remove the reassign statement from the code and then use the append method for the list, For future reference keeps in mind that you only call the append method to the list type objects, not the int type.

Fix 

#no_list is a list type 
no_list = ['1','2','3','4']


#We can see the type 

print('\n',type(no_list),'\n')


#now we will try to append value 5 in the list

no_list.append('5')

print(no_list)

Output:

 

 

 

Also, read Python Tutorials.

 

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *