When programming, you will encounter the AttributeError: ‘dict’ object has no attribute ‘add’. I wrote this post to give you some causes, and workarounds like the add() method must call on set, use set constructor, and add elements on other data types. Read the article below.
What causes the AttributeError: ‘dict’ object has no attribute ‘add’?
Set is a data type in Python related to set theory. A set can contain many elements which are not in any order. The elements of a set are immutable data such as int, string, or tuple. Sets can be declared with braces { } and elements separated by commas.
Dictionary is a relatively commonly used data type in Python. It stores data in the form of key:value pairs. The dictionary can be declared with curly braces { } contains key:value pairs inside, separated by a comma.
The add() method:
- Syntax: set.add(The element to add to the set)
- The set() function has the effect of adding an element to the set. If the element is already present, do nothing.
Note: The initialization with curly braces { } in both data types leads to confusion and errors.
The AttributeError: ‘dict’ object has no attribute ‘add’ occurs because you call the add() method in the dictionary. Add() is the method that must be called on the set.
Example:
dictObj = { 'Rolf': 24, 'Adam': 30, 'Anne': 27 } # Use the add() method. dictObj.add('John') print(dictObj)
Output:
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
AttributeError: 'dict' object has no attribute 'add'
How to solve this error?
The add() method must be called on the set
Example:
# Create a set. valuesSet = { 'Rolf', 'Adam', 'Anne' } print('Initial set:', valuesSet) # Use the add() method to add an element to the set. valuesSet.add('John') print('Set after adding element:', valuesSet)
Output:
Initial set: {'Adam', 'Rolf', 'Anne'}
Set after adding element: {'Adam', 'Rolf', 'John', 'Anne'}
Note: If you add an element already present in the set, the set will remain the same value as the original.
Example:
# Create a set. valuesSet = { 'Rolf', 'Adam', 'Anne' } print('Initial set:', valuesSet) # Use the add() method to add an element to the set. mySet.add('Rolf') print('Set after adding element:', valuesSet)
Output:
Initial set: {'Rolf', 'Anne', 'Adam'}
Set after adding element: {'Rolf', 'Anne', 'Adam'}
Use the set() Constructor
You can create a set using the set() function.
Syntax:
set((Elements in the set))
Example:
# Create a set using the set() function. valuesSet = set(('Rolf', 'Anne', 'Adam')) print('Initial set:', valuesSet) # Use the add() method to add an element to the set. valuesSet.add('John') print('Set after adding element', valuesSet)
Output:
Initial set: {'Adam', 'Rolf', 'Anne'}
Set after adding element {'Adam', 'Rolf', 'John', 'Anne'}
Add an element on the list
If you don’t want to add an element on the set, do it on the list using the append() method.
Example:
valueslist = ['Rolf', 'Anne', 'Adam'] print('Initial list:', valueslist) # Use the append() method to add an element at the end of the list. valueslist.append('John') print('List after adding element', valueslist)
Output:
Initial list: ['Rolf', 'Anne', 'Adam']
List after adding element ['Rolf', 'Anne', 'Adam', 'John']
Summary
My post is over. AttributeError: ‘dict’ object has no attribute ‘add’ is an easy topic to fix. You can choose either way because the three methods are relatively similar. Thank you for taking the time to read this post.
Maybe you are interested:
- AttributeError: ‘str’ object has no attribute ‘loads’
- AttributeError: ‘int’ object has no attribute ‘isdigit’
- AttributeError: ‘list’ object has no attribute ‘join’

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java