Attributeerror: list object has no attribute items ( Solved )

A list is a data structure that contains multiple items in a single variable. Let’s say you want to print out all the items inside the list and use the items() function. In this case, you will encounter with error attributeerror: list object has no attribute items. In this post, you will be able to solve this error easily.

What causes Attributeerror: list object has no attribute items?

Suppose you want to get the items inside the list by applying items() function on the list then you will get the list object has no attribute items error. Let’s understand it in detail by running in the below lines of code.

my_list = ["abc","def","ghi"]
my_list.items()

Output

list object has not attribute items error
list object has no attribute items error

If you also output all the functions provided by the list() object then you will not see the items() function.

dir(list)
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']

Solve list object has no attribute items

To solve or avoid this items() error you have to first convert the list to the dictionary as the items() function is provided in the dictionary data structure.  To use the items() function on the list you have to convert the list to the dictionary.

Now You will not get the error when you will run the below lines of code.

my_list = ["abc","def","ghi"]
my_dict ={}
for i in range(len(my_list)):
  my_dict.update({i:my_list[i]})
print(my_dict.items())

Output

Not getting items() error
Not getting items() error

Conclusion

The items() function only works on the dictionary, not on the list. If you are getting the list object has no attribute items error then you have to first convert the list to the dictionary and then apply the items() function. The above solution will solve your error.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link