AttributeError: 'set' object has no attribute 'items'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'set' object has no attribute 'items' #

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": "Alice"}.

attributeerror set object has no attribute items

Here is an example of how the error occurs.

main.py
employee = {'name', 'Alice', '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.

main.py
employee = {'name': 'Alice', 'age': 30} # 👇️ <class 'dict'> print(type(employee)) # 👇️ dict_items([('name', 'Alice'), ('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.

The 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.

main.py
employee = {'name: Alice, 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 th error in this scenario, we have to split the key-value pairs.

main.py
employee = {'name': 'Alice', 'age': 30} # 👇️ <class 'dict'> print(type(employee)) # 👇️ dict_items([('name', 'Alice'), ('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.

Here is an example of what printing the attributes of a set looks like.

main.py
# 👇️ set object employee = {'name', 'Alice', '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.

If you try to access any attribute that is not in this list, you would get the "AttributeError: set object has no attribute error".

If you do the same for a dict object, you will see the items() method in the list of attributes.

main.py
# 👇️ dict object employee = {'name': 'Alice', 'age': 30} # 👇️ [... 'items', ...] print(dir(employee))

Conclusion #

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": "Alice"}.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.