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

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# 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": "Bobby Hadz"}.

attributeerror set object has no attribute items

Here is an example of how the error occurs.

main.py
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.

# Separate the keys and values of the object with colons

To solve the error, separate the keys and values of the object with colons.

main.py
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)

separate keys and values of object with colons

The code for this article is available on GitHub

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

# Wrapping your keys and values in a string

The error might also occur if you wrap your key-value pairs in a string.

main.py
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.

# Declare individual comma-separated, key-value pairs

To solve the error in this scenario, we have to split the key-value pairs.

main.py
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)

declare individual comma separated key value pairs

The code for this article is available on GitHub

A good way to start debugging is to print(dir(your_object)) and see what attributes the object has.

# Make sure your HTTP request headers are a dictionary

The error commonly occurs when you try to declare a headers dictionary when using the requests module.

main.py
# โœ… Correctly declaring headers dictionary headers = { 'Content-Type': 'application/json', 'Authorization': 'token MY_TOKEN', } print(type(headers)) # ๐Ÿ‘‰๏ธ <class 'dict'>
The code for this article is available on GitHub

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.

main.py
# โ›”๏ธ 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:

  1. Each key has to be wrapped in a string.
  2. Each key-value pair has to be separated by a colon.
  3. There has to be a comma between the key-value pairs.

# Printing the attributes of a Set object

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

main.py
# ๐Ÿ‘‡๏ธ Set object employee = {'name', 'Bobby Hadz', 'age', 30} # ๐Ÿ‘‡๏ธ [...'add', 'clear', 'copy', 'difference', 'difference_update', 'discard'...] print(dir(employee))
The code for this article is available on GitHub

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 will get the "AttributeError: set object has no attribute error".

# Printing the attributes of a dict object

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': 'Bobby Hadz', 'age': 30} # ๐Ÿ‘‡๏ธ [... 'items', ...] print(dir(employee))

As shown in the code sample, the dict object has an items attribute.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev