attributeerror: ‘list’ object has no attribute ‘values’

One of the most common errors that Python programmers encounter is the AttributeError: ‘list’ object has no attribute ‘values’.

In this article, we will discuss the solutions on how to solve it and what the causes of this error are.

Why the error attributeerror: list object has no attribute values occur?

The error message attributeerror: list object has no attribute values occur because you are trying to call the values() method on a list instead of a dictionary.

For example on how the error occurs:

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

name_list = [
    {'id': 1, 'fullname': 'Caren'},
    {'id': 2, 'fullname': 'Glenn'},
    {'id': 3, 'fullname': 'Elijah'},
    {'id': 4, 'fullname': 'Eliver'},
    {'id': 5, 'fullname': 'Jude'},
]
print(name_list.values())

If you run the code above the output will show an error:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 8, in
print(my_list.values())
AttributeError: ‘list’ object has no attribute ‘values’

We generate a list with five dictionaries, and we are trying to call the values() methods on the list. It will cause an error because values() is a dictionary method.

Also read:

These are the following solutions to solve the attributeerror: list object has no attribute values.

Solution 1: Use specific index to access a list element

The best way to solve this error is to access a list element at a specific index.

For example:

We will access the list element at index 0 before calling the dict.values() method.

name_list = [
    {'id': 1, 'firstname': 'Caren'},
    {'id': 2, 'firstname': 'Glenn'},
    {'id': 3, 'firstname': 'Elijah'},
    {'id': 4, 'firstname': 'Eliver'},
    {'id': 5, 'firstname': 'Jude'},
]
values = list(name__list[0].values())
print(values)

Output

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
[1, ‘Caren’]

If we will run the example code above it will print the id number 1 which is caren.

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

For example:

name_list = [
    {'id': 1, 'fullname': 'Caren'},
    {'id': 2, 'fullname': 'Glenn'},
    {'id': 3, 'fullname': 'Elijah'},
    {'id': 4, 'fullname': 'Eliver'},
    {'id': 5, 'fullname': 'Jude'},
]
for person in name_list:
    print(person.values())

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
dict_values([1, ‘Caren’])
dict_values([2, ‘Glenn’])
dict_values([3, ‘Elijah’])
dict_values([4, ‘Eliver’])
dict_values([5, ‘Jude’])

if you run the example above it will print all the name inside in the name_list such as caren, glenn, elijah, eliver and jude.

Let’s take a look at the other example:

dict = {'id': 1,  'name': 'Caren Manggana'}

print(dict.values())

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
dict_values([1, ‘Caren Manggana’])

This is the equality comparison between one dict.values() view and another, which will always return False. This also applies if you compare dict.values() to itself.

Solution 2: Find dictionary list before calling values()

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

For example:

name_list = [
    {'id': 1, 'fullname': 'Caren'},
    {'id': 2, 'fullname': 'Glenn'},
    {'id': 3, 'fullname': 'Elijah'},
    {'id': 4, 'fullname': 'Eliver'},
    {'id': 5, 'fullname': 'Jude'},
]

result = next(
    (item for item in name_list if item['fullname'] == 'Caren'),
    {}
)

print(result)

print(result.get('fullname'))
print(result.get('id'))

print(result.values())

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
{‘id’: 1, ‘fullname’: ‘Caren’}
Caren
1
dict_values([1, ‘Caren’])

The generator expression in the above example will look for a dictionary with a fullname that has the value of “caren” and will return the dictionary.

Conclusion

In conclusion, the “AttributeError: ‘list’ object has no attribute ‘values‘” error message is a common error that can occur when you are running Python program.

It shows that you are trying to use the “values” method on a list object, which does not have that attribute.

Leave a Comment