How To Fix The AttributeError: ‘Set’ Object Has No Attribute ‘Items’ In Python?

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

In this article, we will show you how to fix the AttributeError: ‘set’ object has no attribute ‘items’ in Python and give you some common causes of the error. Continue reading the article for details.

Why does the AttributeError: ‘set’ object has no attribute ‘items’ in Python occur? How to fix it?

AttributeError occurs when you try to access a property on an object that does not have that property defined. Reading the AttributeError error message can help you quickly identify which attribute you tried to access and quickly fix it.

Some common cases cause the error:

Confusion between set and dictionary

Set and dictionary have different syntaxes. The set does not use ‘:‘, and the dictionary uses ‘:‘ to separate the key and value.

# Confused between set and dictionary
websites = {
    "learn", "learnshareit.com", "relax", "youtube.com", "social", "facebook.com"
}

print(websites.items())

Error:

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

In the example above, you might expect the websites to be a dictionary and try to call the items() method. But the websites are a set, and the set doesn’t have an items() method. That causes the AttributeError.

To fix the above error, we need to clearly distinguish the key and value of the dictionary to put the ‘:‘ accordingly. Like this:

# Websites is a dict
websites = {
    "learn": "learnshareit.com",
    "relax": "youtube.com",
    "social": "facebook.com"
}

print(websites.items())

Output:

dict_items([('learn', 'learnshareit.com'), ('relax', 'youtube.com'), ('social', 'facebook.com')])

Wrap a dictionary with quotes

In this case, we have separated the key and its value with ‘:‘, but we enclose them in double quotes so that Python will treat them as a set.

# Wrap a dict with double quotes
websites = {
    "'learn': 'learnshareit.com', 'relax': 'youtube.com', 'social': 'facebook.com'"
}

print(websites.items())

Or:

# Wrap a dict with single quotes
websites = {
    '"learn": "learnshareit.com", "relax": "youtube.com", "social": "facebook.com"'
}

print(websites.items())

Error:

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

To fix the error in this case, we need to remove the quotes wrapped around the dictionary’s contents, and that’s it.

# Removed double quotes
websites = {
    'learn': 'learnshareit.com', 'relax': 'youtube.com', 'social': 'facebook.com'
}

print(websites.items())

Output:

dict_items([('learn', 'learnshareit.com'), ('relax', 'youtube.com'), ('social', 'facebook.com')])

Use try … except to avoid the AttributionError

We introduced the syntax try ... except here. You can read more.

Using the try ... except helps us to fix the AttributeError regardless of the cause. When an error occurs, the code in the except block will execute instead of an error message. Take a close look at the example below for how to use try ... except for this case.

websites = {
    '"learn": "learnshareit.com", "relax": "youtube.com", "social": "facebook.com"'
}

try:
    print(websites.items())
except AttributeError:
    print("The object is having a problem. Please check if the data type you are using is appropriate!!!")

Output:

The object is having a problem. Please check if the data type you are using is appropriate!!!

If you encounter the above message, you must consider continuing to use the items() method or changing another data type for the object.

Summary

To fix the AttributeError: ‘set’ object has no attribute ‘items’ in Python above, we need to look closely at our code to ensure that the dictionary’s syntax is correct. We hope this short article will be of help to you. Have a nice day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *