Select Page

How to Solve Python AttributeError: ‘tuple’ object has no attribute ‘append’

by | Programming, Python, Tips

In Python, a tuple is a built-in data type used to store collections of data. A tuple is a collection that is ordered and unchangeable. Once you create a tuple, you cannot add or remove items.

The append() method appends an element to the end of a list.

If you call the append() method on a tuple, you will raise the AttributeError: ‘tuple’ object has no attribute ‘append’.

To solve this error, you can use a list instead of a tuple or concatenate tuples together using the + operator.

This tutorial will go through how to solve this error with code examples.


AttributeError: ‘tuple’ object has no attribute ‘append’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘tuple’ object has no attribute ‘append’” tells us that the tuple object does not have the attribute append(). We will raise this error by calling the append() method on a tuple object. The append() method appends an element to the end of a list.

The tuple data type is immutable, which means once you create a tuple object, you can no longer edit it. Therefore any method that changes an object will not be an attribute of the tuple data type. Let’s look at an example of appending to a list.

a_list = [2, 4, 6]
a_list.append(8)
print(a_list)
[2, 4, 6, 8]

We can create a tuple using parentheses and comma-separated values. Let’s see what happens when we try to append to a tuple

a_tuple = (2, 4, 6)
a_tuple.append(8)
print(a_tuple)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-456aaa7ab0c3> in <module>
      1 a_tuple = (2, 4, 6)
      2 
----> 3 a_tuple.append(8)
      4 
      5 print(a_tuple)
AttributeError: 'tuple' object has no attribute 'append'

Let’s look at another example in more detail.

Example

In this example, we have a tuple of strings representing types of animals. We want to add another string to the tuple. The code is as follows:

animals = ("cat", "dog", "hedgehog", "bison")
animals.append("otter")
print(f'Tuple of animals: {animals}')

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-ca00b4acd5f9> in <module>
      1 animals = ("cat", "dog", "hedgehog", "bison")
      2 
----> 3 animals.append("otter")
      4 
      5 print(f'Tuple of animals: {animals}')
AttributeError: 'tuple' object has no attribute 'append'

The error occurs because we call the append() method on a tuple. The method append() belongs to the list data type.

Solution #1: Use a List Instead

If we have a container of values and we want to edit the contents during a program’s lifecycle, we should choose a mutable data type such as a list. We have to use square brackets instead of parentheses to define a list. Let’s look at the revised code.

animals = ["cat", "dog", "hedgehog", "bison"]
animals.append("otter")
print(f'List of animals: {animals}')
List of animals: ['cat', 'dog', 'hedgehog', 'bison', 'otter']

We successfully appended the extra string to the list.

Solution #2: Concatenate Tuples

We can emulate appending to a tuple by using tuple concatenation. When we perform tuple concatenation, we create a new tuple object. We can concatenate two tuples using the + operator. Let’s look at the revised code:

animals = ("cat", "dog", "hedgehog", "bison")
animals = animals + ("otter",)
print(f'Tuple of animals: {animals}')

We have to convert the value “otter” to a tuple using parentheses and a comma. Let’s run the code to see the result:

Tuple of animals: ('cat', 'dog', 'hedgehog', 'bison', 'otter')

The result of the concatenation is an entirely new tuple object. Let’s verify this by calling the id() method on the tuple before and after concatenation.

animals = ("cat", "dog", "hedgehog", "bison")
print(id(animals))
animals = animals + ("otter",)
print(id(animals))
print(f'Tuple of animals: {animals}')
140681716882304
140681716860944
Tuple of animals: ('cat', 'dog', 'hedgehog', 'bison', 'otter')

The new tuple has a different id despite being assigned to the same variable. This behaviour is due to the immutability of the tuple data type. However, if we append to a list and call the id() method before and after appending, we will have the same id number:

animals = ["cat", "dog", "hedgehog", "bison"]
print(id(animals))
animals.append("otter")
print(id(animals))
print(f'List of animals: {animals}')
140681729925632
140681729925632
List of animals: ['cat', 'dog', 'hedgehog', 'bison', 'otter']

We get the same ID because the append method changes the list in place; we end up with the same object.

Solution #3: Convert to a List then back to a Tuple

We can convert a tuple to a list using the list() method, then append a value to the list. Once we append the value, we can convert the list to a tuple using the tuple() method. Let’s look at the revised code:

# Define tuple
animals = ("cat", "dog", "hedgehog", "bison")
# Convert tuple to list
animals_list = list(animals)
# Append value to list
animals_list.append("otter")
# Convert list to tuple
animals = tuple(animals_list)
# Print result to console
print(f'Tuple of animals: {animals}')

Let’s run the code to see the result:

Tuple of animals: ('cat', 'dog', 'hedgehog', 'bison', 'otter')

We have successfully appended the extra value to the tuple using list conversion.

Summary

Congratulations on reading to the end of this tutorial. The AttributeError: ‘tuple’ object has no attribute ‘append’ occurs when you try to call the list method append() on a tuple. Tuples are immutable, which means we cannot change them once created. If you expect to add or remove elements from a container during the program’s lifecycle, you should use mutable containers such as lists.

Otherwise, you can convert your extra value to a tuple and concatenate it to the previous tuple using the + operator. You can also convert the tuple you want to change to a list, call the append method, then convert the list back to a tuple.

For further reading on AttributeErrors, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!