Last updated: Apr 10, 2024
Reading timeยท3 min

The Python "AttributeError: 'set' object has no attribute 'items'" occurs when
we create a set object instead of a dictionary.
To solve the error, separate the keys and values in the dictionary by colons
and not commas, e.g. {"name": "Bobby Hadz"}.

Here is an example of how the error occurs.
employee = {'name', 'Bobby Hadz', 'age', 30} print(type(employee)) # ๐๏ธ <class 'set'> # โ๏ธ AttributeError: 'set' object has no attribute 'items' print(employee.items())
Notice that we separated the keys and values in the employee variable by commas instead of colons and ended up creating a Set object.
To solve the error, separate the keys and values of the object with colons.
employee = {'name': 'Bobby Hadz', 'age': 30} # ๐๏ธ <class 'dict'> print(type(employee)) # ๐๏ธ dict_items([('name', 'Bobby Hadz'), ('age', 30)]) print(employee.items()) for k, v in employee.items(): print(k, v)

We used colons to separate the keys and values and created a dictionary.
items() method is specific to dictionaries and returns a view of the dictionary's items ((key, value) pairs).The error might also occur if you wrap your key-value pairs in a string.
employee = {'name: Bobby Hadz, age: 30'} # ๐๏ธ single string print(type(employee)) # ๐๏ธ <class 'set'> # โ๏ธ AttributeError: 'set' object has no attribute 'items' print(employee.items())
Notice that we have a single string between the curly braces in the employee
variable, so we ended up creating a set object.
Since set objects don't have the items() method, the error occurs.
To solve the error in this scenario, we have to split the key-value pairs.
employee = {'name': 'Bobby Hadz', 'age': 30} # ๐๏ธ <class 'dict'> print(type(employee)) # ๐๏ธ dict_items([('name', 'Bobby Hadz'), ('age', 30)]) print(employee.items()) for k, v in employee.items(): print(k, v)

A good way to start debugging is to print(dir(your_object)) and see what
attributes the object has.
The error commonly occurs when you try to declare a headers dictionary when
using the requests module.
# โ Correctly declaring headers dictionary headers = { 'Content-Type': 'application/json', 'Authorization': 'token MY_TOKEN', } print(type(headers)) # ๐๏ธ <class 'dict'>
Notice that each key and each value is wrapped in a string. This is the correct way to declare a dictionary.
The error occurs when you wrap an entire key-value pair in a string.
# โ๏ธ Declares a Set object by mistake headers = { 'Content-Type: application/json', 'Authorization: token MY_TOKEN', } print(type(headers)) # ๐๏ธ <class 'set'>
The code sample wraps an entire key-value pair in a string, so it declares a
Set object.
To declare a dictionary:
Set objectHere is an example of what printing the attributes of a set looks like.
# ๐๏ธ Set object employee = {'name', 'Bobby Hadz', 'age', 30} # ๐๏ธ [...'add', 'clear', 'copy', 'difference', 'difference_update', 'discard'...] print(dir(employee))
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.
dict objectIf you do the same for a dict object, you will see the items() method in the
list of attributes.
# ๐๏ธ `dict` object employee = {'name': 'Bobby Hadz', 'age': 30} # ๐๏ธ [... 'items', ...] print(dir(employee))
As shown in the code sample, the dict object has an items attribute.
You can learn more about the related topics by checking out the following tutorials: