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

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Lydia Shi

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

The Python "AttributeError: 'list' object has no attribute 'keys'" occurs when we call the keys() method on a list instead of a dictionary. To solve the error, call keys() 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 keys

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 'keys' print(my_list.keys())

We created a list with 3 dictionaries and tried to call the keys() method on the list which caused the error because keys() 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].keys()) print(result) # 👉️ ['id', 'name']

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

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

A dictionary is a collection of key-value pairs wrapped in curly braces, whereas a list is a sequence of comma-separated items.

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.keys()) # 👉️ dict_keys(['id', 'name'])

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 'keys'" occurs when we try to call the keys() 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 keys() is not a method implemented by lists, the error is caused.

Use the search field on my Home Page to filter through my more than 1,000 articles.