Last updated: Apr 8, 2024
Reading timeยท7 min
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.
Here is an example of how the error occurs.
example = 'bobby', 'hadz' print(type(example)) # ๐๏ธ <class 'tuple'> # โ๏ธ AttributeError: 'tuple' object has no attribute example.sort()
The example
variable stores a tuple of 2 values.
example = 'bobby', 'hadz' print(example) # ๐๏ธ ('bobby', 'hadz')
If you meant to access a list method, use a list instead of a tuple.
example = ['bobby', 'hadz', 'com'] print(type(example)) # ๐๏ธ <class 'list'> example.sort() print(example) # ๐๏ธ ['bobby', 'com', 'hadz']
Lists are wrapped in square brackets []
and tuples are wrapped in parentheses
()
.
You can convert a tuple to a list by using the list()
constructor.
my_tuple = ('bobby', 'hadz', 'com') # โ Convert a tuple to a list my_list = list(my_tuple) print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com'] print(type(my_list)) # ๐๏ธ <class 'list'>
The list class takes an iterable and returns a list object.
If you created the tuple by mistake, you have to correct the assignment.
Tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorIf you meant to access an element at a specific index in a tuple, use square brackets.
my_tuple = ('bobby', 'hadz', 'com') print(my_tuple[0].upper()) # ๐๏ธ "BOBBY" print(my_tuple[1].upper()) # ๐๏ธ "HADZ"
0
, and the last item has an index of -1
or len(my_tuple) - 1
.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.
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.
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.
If you meant to create a dictionary, wrap key-value pairs in curly braces.
employee = {'name': 'Bobby Hadz', 'age': 30} print(employee.get('name')) # ๐๏ธ "Bobby Hadzz" print(employee['name']) # ๐๏ธ "Bobby Hadz" print(employee['age']) # ๐๏ธ 30
A dictionary is used to store key-value pairs.
There are only 2 methods that you will likely be using on tuple objects.
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.
list
because tuples are immutable.You can view all the attributes an object has by using the dir()
function.
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 will get the error.
Here are 2 examples of solving the error for specific methods. Click on the link to navigate to the subheading.
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.
Here is an example of how the error occurs.
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.
To solve the error, we have to use a list instead of a tuple.
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.
my_tuple = ('a', 'b') my_list = list(my_tuple) my_list.append('c') print(my_list) # ๐๏ธ ['a', 'b', 'c']
append()
which change the object in place.Note that the append()
method mutates the original list, it doesn't return a
new list.
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.
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.
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 will get the error.
The Python "AttributeError 'tuple' object has no attribute 'split'" occurs
when we call the split()
method on a tuple instead of a string.
To solve the error, correct the assignment of the variable or access the tuple
at a specific index when calling split()
.
Here is an example of how the error occurs.
example = 'bobby,hadz', # ๐๏ธ dangling comma declares tuple print(type(example)) # ๐๏ธ <class 'tuple'> # โ๏ธ AttributeError: 'tuple' object has no attribute 'split' result = example.split(',')
We declared a tuple instead of a string by separating multiple values with a comma.
To be able to use the split()
method, we have to declare a string or access
the tuple at a specific index.
example = 'bobby,hadz' print(type(example)) # ๐๏ธ <class 'string'> # โ๏ธ AttributeError: 'tuple' object has no attribute 'split' result = example.split(',') print(result) # ๐๏ธ ['bobby', 'hadz']
We declared a string, so we were able to use the split()
method to split the
string on each comma.
If you meant to call split()
on an element of a tuple, access it at the
specific index.
my_tuple = ('a,b', 'c,d') result = my_tuple[1].split(',') print(result) # ๐๏ธ ['c', 'd']
We accessed the tuple element at index 1
which is a string, so we were able to
call the split()
method.
0
, and the last item has an index of -1
or len(my_tuple) - 1
.Tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorNote that tuples are immutable, so if you have to mutate a sequence, you have to
use a list
instead.
There are only 2 methods that you will likely be using on tuple objects.
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.
my_tuple = ('bobby', 'hadz', 'com') # ๐๏ธ [... '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 will get the "AttributeError: tuple object has no attribute" error.
You can learn more about the related topics by checking out the following tutorials: