AttributeError: list object has no attribute items ( Solved )

AttributeError_ list object has no attribute items ( Solved )

AttributeError: list object has no attribute items error occurs because of accessing items() function from list object in the place of dict type of object. The items() function returns key values pair of dict object in tuple form. Since there are no key values in the list object.  Also, this item() is not defined in the list Python class.  Hence when we invoke items() function from the list object, It throws the AttributeError.

What is a list?

A list is a data structure that allows you to store multiple variables in a single variable. One main advantage of the list is that it is mutable and elements are inserted in the list in an ordered way.

List vs Dictionary

There are many beginner coders who get confused between a dictionary and a list. The main difference between a list and a dictionary is that list elements as a single value whereas a dictionary always exists in key-value pairs. The list is defined using the square bracket whereas the dictionary is defined using the curly brackets “{}”.

AttributeError: list object has no attribute items ( Solution ) –

There are multiple ways to fix this  List related AttributeError but we will explore the easiest and most applicable ways in this article. Also, these solutions are scenario oriented. So while Appling any of it, please make sure to understand the context of the code.

Solution 1: Convert list to dict by adding order key –

As we know the list contains only values but the dict type object required keys and values. Hence we will use the order /location of the corresponding values in the list as the key of the dict. Here is the code for this conversion.

sample_list=['A','B','C']
sample_dict ={}
for i in range(len(sample_list)):
  sample_dict [i]=sample_list[i]
print(sample_dict.items())
list object has no attribute item fix by list to dict conversion
list object has no attribute item fix by list to dict conversion

Solution 2: Accessing list element as dict ( if the element is dict )

This solution only works if the element of the list is a dict type. We will extract the element and then invoke items() with it. Let’s explore this with an example.

sample_list=[{1:'A', 2:'B'},
    {1:'C', 2:'D'}]
sub=sample_list[0].items()
print(sub)
list object has no attribute item fix by accessing list element as dict
list object has no attribute item fix by accessing list element as dict

Solution 3: Replacing items() without loosing purpose

Most of the time we use the items() function because we need the element of the list in tuples. This we can easily get by converting the list to a tuple.

sample_list=['A','B','C']
sample_tuple=tuple(sample_list)
print(sample_tuple)
list object has no attribute item fix by simial logic replacement
list object has no attribute item fix by similar logic replacement

Similar Articles :

AttributeError: list object has no attribute len ( Fixed )

AttributeError: list object has no attribute shape ( Solved )

 

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner