In Python, the list data structure stores elements in sequential order. We can use the dictionary items() method to return a view object containing the key-value pairs of a dictionary.

However, we cannot apply the items() method to a list. If you try to use the items() method on a list, you will raise the error “AttributeError: ‘list’ object has no attribute ‘items’”.

This tutorial will go into detail on the error definition. We will go through an example that causes the error and how to solve it.


AttributeError: ‘list’ object has no attribute ‘items’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘list’ object has no attribute ‘items’” tells us that the list object we are handling does not have the items attribute. We will raise this error by calling the items() method on a list object. items() is a dictionary method that returns a view object containing the key-value pairs of a dictionary as a list of tuples.

The syntax for the items() method is

dictionary.items()

Let’s look at an example of calling the items() method on a dictionary. We can convert the view object into a list using the list() method:

pizza_dict = {"margherita":4, "pepperoni":2, "four cheeses":8}

print(list(pizza_dict.items()))
[('margherita', 4), ('pepperoni', 2), ('four cheeses', 8)]

Now we will see what happens if we try to use the items() method on a list:

pizza_list = [("margherita",4), ("pepperoni",2), ("four cheeses",8)]
print(list(pizza_list.items()))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-3b2fe41217ff> in <module>
      1 pizza_list = [("margherita",4), ("pepperoni",2), ("four cheeses",8)]
----> 2 print(list(pizza_list.items()))

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

The Python interpreter throws the AttributeError because the list object does not have items() as an attribute.

Example: Getting Key-Value Pairs from a List of Dictionaries

This error can typically occur when trying to retrieve values from JSON data. A JSON will give us a list of dictionaries, not a single dictionary. Therefore, we must access each dictionary individually, not the entire list. Let’s look at an example where we have a JSON containing the inventory of a pet store. Each dictionary has three animal names as keys and the number of the animals as the values. We want to get a list of all animal numbers.

pet_store_data = [
{
    "dog":17, 
    "cat":4, 
    "rabbit":8
},
{
    "lizard":1, 
    "snake":4, 
    "dragon":2
},
{
    "fish":20, 
    "frog":6, 
    "toad":1
}
]

total_animal_numbers = list(pet_store_data.items())

print(f'Total number of animals in pet store: {sum(total_animal_numbers)}')

We attempt to call the items() method on the list and then sum the values to get the number of animals in the pet store. Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-36381cddb975> in <module>
     17 ]
     18 
---> 19 total_animal_numbers = list(pet_store_data.items())
     20 
     21 print(f'Total number of animals in pet store: {sum(total_animal_numbers)}')

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

We get the error because the list contains dictionaries, but the items() method is not an attribute of list objects.

Solution

To solve this error, we need to iterate over the elements in the list. The most concise and Pythonic way to do this is to use list comprehension, and list comprehension offers a shorter syntax for creating a new list based on the values of an existing list.

We will use a list comprehension to create a list containing the values of each dictionary in the pet_store_data list. Let’s look at the revised code:

pet_store_data = [
{
    "dog":17, 
    "cat":4, 
    "rabbit":8
},
{
    "lizard":1, 
    "snake":4, 
    "dragon":2
},
{
    "fish":20, 
    "frog":6, 
    "toad":1
}
]

total_animal_numbers = [int(v) for dct in pet_store_data for k, v in dct.items()]

print(f'Total number of animals in pet store: {sum(total_animal_numbers)}')

The first part of the list comprehension states to get the value from each dictionary. The second part of the list comprehension iterates over each dictionary and calls the items() method to get the values. Let’s run the code to get the correct output:

Total number of animals in pet store: 63

We see that the pet store has 63 animals in its inventory.

Summary

Congratulations on reading to the end of this tutorial! The error “AttributeError: ‘list’ object has no attribute ‘items’” occurs when you try to use the items() function to use a key to retrieve a value from a list instead of a dictionary.

The items() method is suitable for dictionaries. If you have a list of dictionaries and want to use the items() method, ensure that you iterate over each dictionary before calling the method. You can extract the values from a list of dictionaries using list comprehension.

Generally, check the object you are using before calling the items() method.

For further reading on AttributeErrors involving the list object, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!