Attributeerror: ‘tuple’ object has no attribute ‘append’ [Solved]

Are you encountering Attributeerror tuple object has no attribute append?

Particularly, when we are working on programming, and developing new programs we can’t avoid facing this error.

In this article, we will look for solutions, and provide example programs and their causes.

But before that, we will understand this attribute error ‘tuple’ object has no attribute ‘append’ first.

What is Attributeerror: ‘tuple’ object has no attribute ‘append’?

The “AttributeError: ‘tuple’ object has no attribute ‘append'” Python error occurs when we attempt to call the append() method on a tuple instead of a list.

In order to avoid this error we should use the list instead of a tuple since tuples is immutable.

Certainly, Tuples are very identical to lists but execute fewer built-in methods and are immutable (cannot be changed).

Here is how the error occurs:

my_list = ('It', 'sourcecode')

print(type(my_list))  # <class 'tuple'>

# AttributeError: 'tuple' object has no attribute 'append'
my_list.append('website')

The code shows we created a tuple object instead of a list since we use parenthesis to wrap comma-separated elements.

Therefore we get the following result.

Attributeerror tuple object has no attribute append example error
Attributeerror ‘tuple’ object has no attribute ‘append’ example error

How to fix Attributeerror: ‘tuple’ object has no attribute ‘append’

Here are the solutions to fix the error Attributeerror: ‘tuple’ object has no attribute ‘append’

1. Use a list instead of a tuple

The quickest way to fix this error ‘tuple’ object has no attribute ‘append’ is to use a list instead of a tuple. All we have to do is to wrap the items in square brackets to enable us to create a list.

Thus, we can now call the append() method and add an item to the end of the list.

my_list = ['it', 'itsc']

print(type(my_list))  # <class 'list'>

my_list.append('sourcecode')

print(my_list)  # ['it', 'itsc', 'sourcecode']

Output:

<class 'list'>
['it', 'itsc', 'sourcecode']

Keep in mind: To create empty list use square brackets [] instead of parentheses.

2. Convert a tuple to a list

This time we will convert tuple into list using with list() contructor.
Since the tuple objects are immutable there are only few methods we can use.

As a result, append() method does not implement tuple objects.

Example code:

my_tuple = ('it', 'itsc')

my_list = list(my_tuple)

my_list.append('sourcecode')

print(my_list)  # ['it', 'itsc', 'sourcecode']

Keep in mind that append() method is mutable or changes the original list, yet it doesn’t return the new list.

Additionally, append() method returns None, so don’t assign the result of calling it to a variable.

Output:

[‘it’, ‘itsc’, ‘sourcecode’]

Two methods you can use tuple object

There are only 2 methods that you will likely be using on tuple objects.

  1. count method
    • It is the method where it returns the number of occurrences of a value in the tuple.
  2. index method
    • It is the method where it returns the index of the tuple value.
my_tuple = ('I', 'T', 'S', 'C')

print(my_tuple.count('C'))  # 1
print(my_tuple.index('I'))  # 0

Output:

1
0

Use dir() function

To view, all attributes of the object use the dir() function.

This time if you pass the class to dir() function, it returns a list of names of the class’s attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the error.

Example:

my_tuple = ('i', 't', 's', 'c')

# [... 'count', 'index' ...]
print(dir(my_tuple))

Output:

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

Causes of Attributeerror: ‘tuple’ object has no attribute ‘append’

Here are some possible causes of the “AttributeError: ‘tuple’ object has no attribute ‘append'” error in Python:

  • Attempting to use the “append()” method on a tuple, which is a collection of elements that cannot be modified.
  • Forgetting to convert a tuple to a list before trying to modify it using the “append()” method.
  • Using parentheses () instead of square brackets [] to create a list, resulting in a tuple being created instead.
  • Mistakenly assigning a tuple to a variable name that was previously used for a list, leads to confusion and errors.
  • Using a library function or method that returns a tuple instead of a list, and assuming that it can be modified using “append()”.

Conclusion

In conclusion, we should be mindful of utilizing the Python built-in functions. Particularly, be aware of the type of variable we are using and whether it is acceptable in the method we are using.

Hence the solution we provided above will fix the AttributeError: ‘tuple’ object has no attribute ‘append’ error. Choose what works best for you.

If you are finding solutions to some errors you might encounter we also have  Typeerror: can’t compare offset-naive and offset-aware datetimes.

Leave a Comment