AttributeError: 'dict' object has no attribute 'predictors'

#Try without dot notation
sample_dict = {'name': 'John', 'age': 29}
print(sample_dict['name']) # John
print(sample_dict['age']) # 29

The dict.items iterates over the key-value pairs of a dictionary. Therefore for key, value in dictionary.items() will loop over each pair. This is documented information and you can check it out in the official web page, or even easier, open a python console and type help(dict.items). And now, just as an example:

>>> d = {'hello': 34, 'world': 2999}
>>> for key, value in d.items():
...   print key, value
...
world 2999
hello 34

The AttributeError is an exception thrown when an object does not have the attribute you tried to access. The class dict does not have any predictors attribute (now you know where to check it :) ), and therefore it complains when you try to access it. As easy as that.


product_details = {
'name':'mobile',
'company':'samsung'}

accessing product_details.name will throw the error "dict object has no attribute 'name' " . reason is because we are using dot (.) to access dict item.

right way is :
product_details['name']  

we use dot operator to access values from objects in python.

the dictionary.items() allows us to loop through key:value pairs in the dictionary

for key, value in product_details.items(): 
    print(key,':',value)

for each iteration of the loop a key and its value is assigned to the variables key and value here. this is how items() method works.

Tags:

Python