How to Solve "AttributeError: 'set' object has no attribute 'items'" in Python
The error "AttributeError: 'set' object has no attribute 'items'" in Python occurs when you try to use the .items()
method on a set
object. The .items()
method is specific to dictionaries, not sets.
This guide explains why this error happens and, most importantly, how to define your dictionaries correctly to avoid it.
Understanding the Error: Sets vs. Dictionaries
The error arises from a fundamental misunderstanding of the difference between Python sets and dictionaries:
-
Sets: Sets are unordered collections of unique items. They are defined using curly braces
{}
with elements separated by commas. Sets do not have key-value pairs.my_set = {'apple', 'banana', 'cherry'} # This is a SET
print(type(my_set)) # Output: <class 'set'> -
Dictionaries: Dictionaries store key-value pairs. Keys must be unique and immutable (like strings, numbers, or tuples), and values can be of any type. Dictionaries are also defined using curly braces
{}
, but key-value pairs are separated by colons (:
), and pairs are separated by commas.my_dict = {'name': 'Alice', 'age': 25} # This is a DICTIONARY
print(type(my_dict)) # Output: <class 'dict'>
The .items()
method is a dictionary method. It returns a view object containing the dictionary's key-value pairs as tuples. Sets do not have this method, hence the AttributeError
.
Correcting Dictionary Syntax
The most common cause of this error is incorrect dictionary syntax. Make sure you are using:
- Key-Value Pairs with Colons: Each item in a dictionary must be a
key: value
pair. - Commas Between Key-Value Pairs: Separate each key-value pair with a comma.
Incorrect (creates a set):
# ⛔️ This creates a SET, not a dictionary!
employee = {'name', 'Tom Nolan', 'age', 25}
print(type(employee)) # Output: <class 'set'>
# ⛔️ AttributeError: 'set' object has no attribute 'items'
# print(employee.items())
Correct (creates a dictionary):
employee = {'name': 'Tom Nolan', 'age': 25} # Correct dictionary syntax
print(type(employee)) # Output: <class 'dict'>
print(employee.items()) # Output: dict_items([('name', 'Tom Nolan'), ('age', 25)])
for k, v in employee.items():
print(k, v)
Output:
name Tom Nolan
age 25
Key differences:
- The key-value pairs are defined as
'key': value
- The key-value pairs are separated by commas.
- You can not have duplicate keys.
- The keys have to be immutable types.
Another common mistake: is to wrap an entire key-value pair in a string, which will create a set
object, if curly braces are used.
# Creates a set
headers = {
'Content-Type: application/json',
'Authorization: token MY_TOKEN',
}
print(type(headers)) # Output: <class 'set'>
Make sure to use the correct syntax when defining a dictionary.
Checking the Object Type
If you're unsure whether a variable holds a set or a dictionary, use the type()
function or isinstance()
to check:
employee = {'name', 'Tom Nolan', 'age', 25} #Incorrectly defined
print(type(employee)) # Output: <class 'set'> (It's a set!)
print(isinstance(employee, dict)) # Output: False (It's NOT a dictionary)
If type(employee)
shows <class 'set'>
, you've accidentally created a set. Review your code and correct the syntax to use the proper dictionary format ({key1: value1, key2: value2, ...}
).
Conclusion
The AttributeError: 'set' object has no attribute 'items'
error occurs when you mistakenly create a set
instead of a dict
and then try to use dictionary-specific methods like .items()
.
- The solution is to ensure that you are using the correct dictionary syntax:
{key1: value1, key2: value2, ...}
. - Always double-check your curly braces, colons, and commas, and use
type()
orisinstance()
to verify the object type if you're unsure.