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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

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

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 'get' my_list.get('name')

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

We accessed the list element at index 0 and used the get() method to get the value of the name key.

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

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)

If you need to use the get() method on each dictionary in a list, use a for loop to iterate over the list.

main.py
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] new_list = [] for person in my_list: new_list.append(person.get('name')) print(person.get('name')) print(new_list) # 👉️ ['Alice', 'Bob', 'Carl']

We used a for loop to iterate over the list and on each iteration used the get() method to get the value of the name property of the dictionary.

The example can be rewritten using a list comprehension.

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

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)

If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

The "AttributeError: 'list' object has no attribute 'get'" occurs when we try to call the get() 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 class's 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 get() is not a method implemented by lists, the error is caused.

Conclusion #

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

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.