How to Solve Python AttributeError: ‘dict’ object has no attribute ‘add’

by | Programming, Python, Tips

To define an empty set in Python 3, you need to use the built-in set() function. If instead, you use the curly brackets {}, you are creating an empty dictionary. Then when you try to use the set method add on the empty dictionary, you will raise the AttributeError: ‘dict’ object has no attribute ‘add’. The add method belongs to the set data type and not the dictionary data type.

To solve this error, ensure the object you create is a set using the built-in set() method.

This tutorial will go through the error in detail and solve it with the help of code examples.


AttributeError: ‘dict’ object has no attribute ‘add’

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 “‘dict’ object has no attribute ‘add’” tells us that the dictionary object we are handling does not have the add attribute.

The add method belongs to the set data type and adds an element to the set. The syntax of add is as follows.

set.add(element)

Parameters

  • element: Required. The element to add to the set

Let’s look at an example of adding an element to a set:

vegetables = {"spinach", "broccolli", "asparagus"}

vegetables.add("courgette")

print(vegetables)
{'broccolli', 'courgette', 'asparagus', 'spinach'}

Let’s look at what happens when we call the add method on a dictionary in the next section.

Example

This error commonly occurs when incorrectly creating an empty set. Let’s look at an example where we try to create a set and add elements to it:

fruits = {}

print(type(fruits))

fruits.add("apple")

fruits.add("banana")

fruits.add("blueberry")

print(f'Set of fruits: {fruits}')
<class 'dict'>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-52a4cc68d1dd> in <module>
      3 print(type(fruits))
      4 
----> 5 fruits.add("apple")
      6 
      7 fruits.add("banana")
AttributeError: 'dict' object has no attribute 'add'

The error occurs because we use the curly brackets {} to create a set. The use of curly brackets creates an empty dictionary. We use a print statement for the type of the fruits object, and we see it prints <class ‘dict’>. The dictionary data type does not have add as a method.

Solution

We need to use the built-in set() method to create an empty set to solve this error. Let’s look at the revised code:

fruits = set()

print(type(fruits))

fruits.add("apple")

fruits.add("banana")

fruits.add("blueberry")

print(f'Set of fruits: {fruits}')
<class 'set'>
Set of fruits: {'banana', 'apple', 'blueberry'}

From the first print statement, we see that we have an empty set that we can fill up using the add method. The final print statement shows the set with three elements.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: ‘dict’ object has no attribute ‘add’ occurs when you call the add() method on a dictionary. This error typically occurs when you create a set using curly brackets instead of the set() method.

To solve this error, use the set() method to create an empty set. If this error occurs in your program, add print statements to print the data type of objects you create.

For further reading on AttributeErrors involving the list object, 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!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee