AttributeError: 'tuple' object has no attribute 'append'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'tuple' object has no attribute 'append' #

The Python "AttributeError: 'tuple' object has no attribute 'append'" occurs when we try to call the append() method on a tuple instead of a list. To solve the error, use a list instead of a tuple because tuples are immutable.

attributeerror tuple object has no attribute append

Here is an example of how the error occurs.

main.py
my_list = ('a', 'b') print(type(my_list)) # 👉️ <class 'tuple'> # ⛔️ AttributeError: 'tuple' object has no attribute 'append' my_list.append('c')

We used parentheses to wrap the comma-separated elements, so we ended up creating a tuple object.

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

To solve the error, we have to use a list instead of a tuple.

main.py
my_list = ['a', 'b'] print(type(my_list)) # 👉️ <class 'list'> my_list.append('c') print(my_list) # 👉️ ['a', 'b', 'c']

We wrapped the items in square brackets to create a list and we were able to call the append() method to add an item to the list.

Note that to create an empty list, you would use square brackets, e.g. my_list = [] and not parentheses.

You can convert a tuple into a list by using the list() constructor.

main.py
my_tuple = ('a', 'b') my_list = list(my_tuple) my_list.append('c') print(my_list) # 👉️ ['a', 'b', 'c']
Tuple objects implement very few methods because they are immutable (cannot be changed). This is the reason tuple object's don't implement methods like append() which change the object in place.

Note that the append() method mutates the original list, it doesn't return a new list.

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

If you need to mutate the sequence, you have to use a list because tuples are immutable.

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

main.py
my_tuple = ('a', 'b', 'c', 'c') print(my_tuple.count('c')) # 👉️ 2 print(my_tuple.index('a')) # 👉️ 0

The count method returns the number of occurrences of the value in the tuple and the index method returns the index of the value in the tuple.

You can view all the attributes an object has by using the dir() function.

main.py
my_tuple = ('a', 'b', 'c', 'c') # 👇️ [... 'count', 'index' ...] print(dir(my_tuple))

If you pass a class to the 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 "AttributeError: tuple object has no attribute" error.

Conclusion #

The Python "AttributeError: 'tuple' object has no attribute 'append'" occurs when we try to call the append() method on a tuple instead of a list. To solve the error, use a list instead of a tuple because tuples are immutable.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.