The AttributeError: ‘list’ object has no attribute ‘get’ mainly occurs when you try to call the get()
method on the list data type. The attribute get()
method is present in the dictionary and must be called on the dictionary data type.
In this tutorial, we will look at what exactly is AttributeError: ‘list’ object has no attribute ‘get’ and how to resolve this error with examples.
What is AttributeError: ‘list’ object has no attribute ‘get’?
If we call the get()
method on the list data type, Python will raise an AttributeError: ‘list’ object has no attribute ‘get’. The error can also happen if you have a method which returns an list instead of a dictionary.
Let us take a simple example to reproduce this error.
# Method return list of dict
def fetch_data():
cars = [
{'name': 'Audi', 'price': 45000},
{'name': 'Ferrari', 'price': 450000},
{'name': 'BMW', 'price': 55000},
]
return cars
data = fetch_data()
print(data.get("name"))
Output
AttributeError: 'list' object has no attribute 'get'
In the above example, we have a method fetch_data()
which returns an list of dictionary object instead of a dictionary.
Since we call the get()
method directly on the list type, we get AttributeError.
We can also check if the variable type using the type()
method, and using the dir()
method, we can also print the list of all the attributes of a given object.
# Method return list of dict
def fetch_data():
cars = [
{'name': 'Audi', 'price': 45000},
{'name': 'Ferrari', 'price': 450000},
{'name': 'BMW', 'price': 55000},
]
return cars
data = fetch_data()
print("The type of the object is ", type(data))
print("List of valid attributes in this object is ", dir(data))
Output
The type of the object is <class 'list'>
List of valid attributes in this object is ['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
How to fix AttributeError: ‘list’ object has no attribute ‘get’?
Let us see how we can resolve the error.
Solution 1 – Call the get() method on valid dictionary
We can resolve the error by calling the get()
method on the valid dictionary object instead of the list type.
Since the data has a valid dictionary object inside the list
, we can loop through the list and use the get()
method on the dictionary elements.
The dict.get()
method returns the value of the given key. The get()
method will not throw KeyError if the key is not present; instead, we get the None
value or the default value that we pass in the get()
method.
# Method return list of dict
def fetch_data():
cars = [
{'name': 'Audi', 'price': 45000},
{'name': 'Ferrari', 'price': 450000},
{'name': 'BMW', 'price': 55000},
]
return cars
data = fetch_data()
for item in data:
print(item.get("name"))
Output
Audi
Ferrari
BMW
We can also use the generator expression
, filter
function to get specific dictionary element, or directly use the index
of the list. Let us look at each of these with examples.
# Method return list instead of dict
def fetch_data():
cars = [
{'name': 'Audi', 'price': 45000},
{'name': 'Ferrari', 'price': 450000},
{'name': 'BMW', 'price': 55000},
]
return cars
data = fetch_data()
# Generator expression to get specific element
car_obj = next(
(x for x in data if x['name'] == 'BMW'),
{}
)
print(car_obj)
print("Car Name is ", car_obj.get("name"))
print("Car Price is ", car_obj.get("price"))
# Directly access the dictionary using the index of list
print("The car name in index 0 is ", data[0].get("name"))
Output
{'name': 'BMW', 'price': 55000}
Car Name is BMW
Car Price is 55000
The car name in index 0 is Audi
Solution 2 – Check if the object is of type dictionary using type
Another way is to check if the object is of type dictionary; we can do that using the type()
method. This way, we can check if the object is of the correct data type before calling the get()
method.
# Method return list of dict
def fetch_data():
cars = [
{'name': 'Audi', 'price': 45000},
{'name': 'Ferrari', 'price': 450000},
{'name': 'BMW', 'price': 55000},
]
return cars
# assigns the list of dict
data = fetch_data()
if (type(data) == dict):
print(data.get("name"))
else:
print("The object is not dictionary and it is of type ", type(data))
# assign the index 0 dict
my_car =data[0]
if (type(my_car) == dict):
print(my_car.get("name"))
else:
print("The object is not dictionary and it is of type ", type(my_car))
Output
The object is not dictionary and it is of type <class 'list'>
Audi
Solution 3 – Check if the object has get attribute using hasattr
Before calling the get()
method, we can also check if the object has a certain attribute. Even if we call an external API which returns different data, using the hasattr()
method, we can check if the object has an attribute with the given name.
# Method return list of dict
def fetch_data():
cars = [
{'name': 'Audi', 'price': 45000},
{'name': 'Ferrari', 'price': 450000},
{'name': 'BMW', 'price': 55000},
]
return cars
# assigns the list of dict
data = fetch_data()
if (hasattr(data, 'get')):
print(data.get("name"))
else:
print("The object does not have get attribute")
# assign the index 0 dict
my_car = data[0]
if (hasattr(my_car, 'get')):
print(my_car.get("name"))
else:
print("The object does not have get attribute")
Output
The object does not have get attribute
Audi
Conclusion
The AttributeError: ‘list’ object has no attribute ‘get’ occurs when you try to call the get()
method directly on the list data type. The error also occurs if the calling method returns an list instead of a dictionary object.
We can resolve the error by calling the get()
method on the dictionary object by iterating the list instead of directly calling the get()
method on an list. We can check if the object is of type dictionary using the type()
method, and also, we can check if the object has a valid get attribute using hasattr()
before performing the get operation.