Skip to content

How to Fix: Python AttributeError: ‘dict’ object has no attribute ‘append’

How to fix Python AttributeError 'dict' object has no attribute 'append' Cover Image

The Python AttributeError is a common one that can mean different things. In this tutorial, you’ll learn how to solve the Python AttributeError: ‘dict’ object has no attribute ‘append’ error. The solution to the error can be interpreted in different ways. This tutorial will guide you through various causes of the error as well as providing solutions to solving it.

By the end of this tutorial, you’ll have learned:

  • What the Python AttributeError means and how to resolve it
  • How to resolve the error by adding new items to Python dictionaries
  • How to append to dictionary keys that are lists

What Does the Python AttributeError: ‘dict’ object has no attribute ‘append’ Mean?

The Python AttributeError: ‘dict’ object has no attribute ‘append’ error occurs when you try to use the .append() method to add items to a Python dictionary. What the error indicates is that Python dictionaries do not have a method called .append().

Let’s see how we can raise the error:

# Raising a Python AttributeError
values = {'a': 1, 'b': 2}
values.append(3)

# Raises: AttributeError: 'dict' object has no attribute 'append'

We can see that by applying the .append() to a Python dictionary, the error is raised.

We can verify that the method doesn’t exist by passing a dictionary into the dir() function:

# Checking Methods Available to Python Dictionaries
values = {'a': 1, 'b': 2}
print(dir(values))

# Returns:
# ['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

By doing this, we can see all of the different attributes and methods that an object has available to it. This allows us to verify that the method doesn’t exist, meaning that the error is raised when we try to apply the method.

In the following three sections, you’ll learn how to resolve the error, depending on the situation you’re trying to use.

How to Resolve the AttributeError by Adding a New Item to a Python Dictionary

In order to add an item to a Python dictionary, you simply need to assign its key-value pair. This means that you do not need to use a method, such as append, in order to add new items to a dictionary. Say you have a dictionary and want to add the key-value pair of 'age':33 to it, you can simply directly assign this value.

Let’s see how you can do this using Python:

# Adding a New Key-Value Pair to a Dictionary
values = {'a': 1, 'b': 2}
values['c'] = 3

print(values)

# Returns: {'a': 1, 'b': 2, 'c': 3}

In doing this, we can see that we don’t actually need to use a method such as .append() to add a new item to a dictionary. Knowing this can prevent many different issues down the road for your code.

How to Resolve the AttributeError by Using a List

If you want to append an item to a Python object directly, you may want to consider switching the data type to a list. Lists are mutable and heterogeneous so you can add new items to them using the .append() method. This allows you to easily add a new item to the end of a list. Let’s see how we can define a new list and add new items to it:

# Adding New Items to a Python List
values = [1, 2, 3]
values.append(4)

print(values)

# Returns: [1, 2, 3, 4]

While this does change the problem statement of this tutorial, it’s a good thing to consider. When you’re simply wanting to add new items to a container, perhaps dictionaries aren’t the data type you were looking for.

How to Resolve the AttributeError by Appending to a List as a Dictionary Value

In some cases, you’ll want to append an item to a list that’s a value in a Python dictionary. For example, you may have a dictionary that looks like this: {'item': [1, 2, 3]}. In this case, it’s perfectly valid to want to append an item to the list that’s contained the in the dictionary.

In these cases, you can use the append method. However, instead of applying the method to the dictionary itself, you apply it to the key of a dictionary. Let’s see what this looks like:

# Appending to a List in a Dictionary
values = {'ages': [23, 32, 34]}
values['ages'].append(64)

print(values)

# Returns:
# {'ages': [23, 32, 34, 64]}

Let’s break down what we did in the code above:

  1. We defined a dictionary that has a single key-value pair, where the value is a Python list
  2. We then used the .append() method on the key of the dictionary
  3. Finally, we printed our updated dictionary, which contained the appended value

Conclusion

In this tutorial, you learned how to resolve the Python AttributeError: ‘dict’ object has no attribute ‘append’ error. The error is raised when you attempt to apply the append method to a Python dictionary. Because Python dictionaries don’t have an append method, you need to use alternative methods to add new items to a dictionary. You first learned how to resolve the error by assigning a new key-value pair directly to the dictionary. Then, you learned how to resolve the error by using a list instead of a dictionary. Finally, you learned how to use the append method to add a new item to a list that’s a dictionary’s value.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *