AttributeError: 'tuple' object has no attribute in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

AttributeError: 'tuple' object has no attribute in Python #

The Python "AttributeError: 'tuple' object has no attribute" occurs when we access an attribute that doesn't exist on a tuple. To solve the error, use a list instead of a tuple to access a list method or correct the assignment.

attributeerror tuple object has no attribute

Here is an example of how the error occurs.

main.py
example = 'hello', 'world' print(type(example)) # 👉️ <class 'tuple'> # ⛔️ AttributeError: 'tuple' object has no attribute 'sort' example.sort()

The example variable stores a tuple of 2 values.

main.py
example = 'hello', 'world' print(example) # 👉️ ('hello', 'world')
Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

If you meant to access a list method, use a list instead of a tuple.

main.py
example = ['hello', 'world'] print(type(example)) # 👉️ <class 'list'> example.sort()

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

main.py
my_tuple = ('a', 'b', 'c') # ✅ convert tuple to list my_list = list(my_tuple) print(my_list) # 👉️ ['a', 'b', 'c'] print(type(my_list)) # 👉️ <class 'list'>

If you created the tuple by mistake, you have to correct the assignment.

Tuples are constructed in multiple ways:

  • Using a pair of parenthesis () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

If you meant to access an element at a specific index in a tuple, use square brackets.

main.py
my_tuple = ('a', 'b') print(my_tuple[0].upper()) # 👉️ "A" print(my_tuple[1].upper()) # 👉️ "B"

If you created the tuple by mistake, track down where the variable got assigned a tuple and correct the assignment.

Here is an example of creating a tuple by mistake by separating values with a comma.

main.py
def get_list(): return 'a', 'b' # 👈️ returns a tuple # 👇️ ('a', 'b') tuple result = get_list() # ⛔️ AttributeError: 'tuple' object has no attribute 'append' result.append('c')

The get_list function returns a tuple instead of a list because we didn't wrap the values in square brackets.

To correct the error in this scenario, wrap the items in square brackets.

main.py
def get_list(): return ['a', 'b'] # 👇️ ['a', 'b'] list result = get_list() result.append('c') print(result) # 👉️ ['a', 'b', 'c']

Make sure you don't have any dangling commas after a value, e.g. a, or (a, ) because you could be creating a tuple by mistake.

Tuple objects have very few built-in methods and are immutable (cannot be changed), so chances are you meant to create a different type of object.

If you meant to create a dictionary, wrap key-value pairs in curly braces.

main.py
employee = {'name': 'Alice', 'age': 30} print(employee.get('name')) # 👉️ "Alice" print(employee['name']) # 👉️ "Alice" print(employee['age']) # 👉️ 30

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.

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

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 classes' 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.

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.