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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

AttributeError: 'list' object has no attribute 'values' #

The Python "AttributeError: 'list' object has no attribute 'values'" occurs when we call the values() method on a list instead of a dictionary. To solve the error, call values() on a dict, e.g. by accessing the list at a specific index or by iterating over the list.

attributeerror list object has no attribute values

Here is an example of how the error occurs.

main.py
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] # ⛔️ AttributeError: 'list' object has no attribute 'values' print(my_list.values())

We created a list with 3 dictionaries and tried to call the values() method on the list which caused the error because values() is a dictionary method.

One way to solve the error is to access a list element at a specific index.

main.py
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] result = list(my_list[0].values()) print(result) # 👉️ [1, 'Alice']

If you need to call the values() method on all dictionaries in the list, use a for loop.

main.py
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] for person in my_list: print(person.values())

The dict.values method returns a new view of the dictionary's values.

main.py
my_dict = {'id': 1, 'name': 'Alice'} print(my_dict.values()) # 👉️ dict_values([1, 'Alice'])

An equality comparison between one dict.values() view and another will always return False. This also applies when comparing dict.values() to itself.

If you need to find a dictionary in a list, use a generator expression.

main.py
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] result = next( (item for item in my_list if item['name'] == 'Bob'), {} ) print(result) # 👉️ {'id': 2, 'name': 'Bob'} print(result.get('name')) # 👉️ "Bob" print(result.get('id')) # 👉️ 2 print(result.values()) # 👉️ dict_values([2, 'Bob'])

The generator expression in the example looks for a dictionary with a name key that has a value of Bob and returns the dictionary.

We used an empty dictionary as a fallback, so if dictionary in the list has a name key with a value of Bob, the generator expression would return an empty dictionary.

If you need to find all dictionaries in the list that match a condition, use the filter() function.

main.py
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Alice'}, ] new_list = list( filter(lambda person: person.get('name') == 'Alice', my_list) ) # 👇️ [{'id': 1, 'name': 'Alice'}, {'id': 3, 'name': 'Alice'}] print(new_list)
The "AttributeError: 'list' object has no attribute 'values'" occurs when we try to call the values() method on a list instead of a dictionary.

You can view all the attributes an object has by using the dir() function.

main.py
my_list = ['a', 'b', 'c'] # 👉️ [... 'append', 'clear', 'copy', 'count', 'extend', 'index', # 'insert', 'pop', 'remove', 'reverse', 'sort' ...] print(dir(my_list))

If you pass a class to the dir() function, it returns a list of names of the classes' attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: list object has no attribute" error.

Since values() is not a method implemented by lists, the error is caused.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.