The error AttributeError: ‘List’ Object Has No Attribute ‘Get’ means you are calling get on a list instead of on a dictionary or similar mapping.
What AttributeError: ‘List’ Object Has No Attribute ‘Get’ Means
When Python raises this message, it signals that the object stored in your variable is a list, yet your code calls the method get() on it. Lists in Python hold items in order and let you read or change them with index access, slicing, and methods such as append() or extend(). A list does not provide the lookup method get(); that method belongs to dictionaries and several dictionary like objects.
This mismatch between object type and method tells you something about the logic flow in your program. Somewhere between creating the data and using it, you treat a list as if it were a mapping from names to values. Understanding how that happened gives you the path to a clean fix.
Most sightings of this error trace back to one of a handful of patterns. Common ones include code that expects a dictionary from a function that sometimes returns a list, view logic in Django that handles request data in more than one shape, or loops that collect items into a list and later try to read values with get().
Why List Object Has No Get Attribute Appears In Python
This kind of AttributeError tends to show up when working with nested data, external input, or libraries that build complex objects. A single endpoint may return either a dictionary or a list depending on parameters. A web form may send one value as a string or many values as a list. Serializers and query helpers may switch between one object and many objects based on conditions. In each case, your code needs to match the method call to the actual object that arrives at that line. That keeps expectations stable.
A quick mindset shift helps here. Instead of thinking of the error as Python being picky, see it as a guard rail. It stops the program the moment your assumption turns false, instead of letting you carry wrong data deeper into the call chain where bugs are harder to trace and fix.
Typical Situations That Trigger The Error
Developers bump into this list AttributeError in several repeatable ways. Once you see these patterns, you can scan your own code for similar spots.
To see the contrast between these shapes, it helps to line them up in one place. The small table below compares lists, dictionaries, and JSON top level arrays so you can match your method calls to the right structure. Skim it before you change code, then use it as a quick check when you are not sure what an API gave back.
| Python Object | Error Symptom | Correct Way To Reach Values |
|---|---|---|
| List | You call obj.get("field") and Python raises an AttributeError saying the list has no get method. |
Use index access like obj[0] or loop through the list with a for statement. |
| Dictionary | You expect a list of items but receive mapping style data with named fields. | Use obj.get("field") when a missing field is allowed, or obj["field"] when the field must exist. |
| JSON Top Level List | An API returns an array at the top level instead of a mapping between names and data objects. | Loop through each entry and treat each entry as its own dictionary with fields and values. |
When you match a failing object to one of these rows, the route to a steady fix becomes easier to see. In some places you will decide to change the producing function so that it shapes the data in a consistent way. In other places you will adjust the calling code so that it respects the actual object type that arrives.
- Using get On Plain Lists — You store data in a list, later forget the shape, and call
data.get("field")out of habit. Since lists work with indexes and not named fields, Python raises the error. - Mixing Up Dictionary And List Returns — A helper returns a dictionary in one branch and a list in another branch. The caller always assumes a dictionary and calls
get(), so the list branch fails. - Flattening Data Too Early — You build a dictionary from parsed data, then pass
dict.values()to another function and store that result. Later calls useget()on the stored object, which is now a list of values. - Misreading Library Return Types — Some APIs return a list of records, others return a dictionary that wraps metadata plus data. Reading the docs closely prevents treating one as the other.
- Handling Single Or Multiple Items — A function may sometimes return one object, sometimes a list of objects. Code that always calls
get()on the result will break when that single object case turns into a list.
Once you can name which pattern matches your bug, you can apply a matching fix instead of patching code at random.
Quick Ways To Confirm You Are Calling Get On A List
A short round of checks often reveals why this AttributeError pops up. The aim here is not to throw print statements all over the module, but to gather just enough detail about the failing object to choose the right repair.
- Print The Type Of The Object — Use
print(type(obj))right before the line that fails. If the output shows, you know the object is a list. - Inspect A Small Sample — Add
print(obj[:3])for a list orprint(list(obj.items())[:3])for a dictionary. The shape of that sample tells you what you have. - Log The Upstream Source — If the value comes from a function, log its return type inside that function too. That helps you see where the transformation into a list took place.
- Check Conditional Branches — Review the parts of your code that build or change this object. Pay special attention to paths that only run in rare cases, such as error handling or optional parameters.
Those small steps keep debugging focused. Once you verify that the failing object is a list and not a dictionary, you can pick one of several concrete repairs.
Fixing The Error When You Control The Data Shape
In many projects you own both the producer and the consumer of the data. That gives you room to align the return type with the method calls in a stable way instead of bolting on patches.
- Return A Dictionary Instead Of A List — If callers expect
result.get("field"), change the function so it always returns a dictionary. Turn lists into dictionaries at the boundary, not in scattered places downstream. - Switch From Get To Indexing — If the data belongs in a list, drop the call to
get()and read items withobj[index]instead. You may add bounds checks to avoidIndexError. - Use Dictionaries For Named Fields — When data carries labeled fields, store it in a dictionary instead of in a list of fixed positions. That matches the use of
get()and makes the code easier to read. - Normalize Return Types — If a function sometimes returns one record and sometimes many, decide on one stable shape. You can always wrap a single record in a list or always return a list and make callers handle both the empty and multi item cases.
- Write Small Helper Functions — Introduce helpers like
ensure_list(obj)orensure_dict(obj)that convert the input into the desired shape at the edge of your module.
When you treat data shape as part of your interface, methods like get() become clear signals. They say “this piece is a mapping,” and that expectation stays aligned through the code base.
Fixing AttributeErrors From Popular Libraries
Some of the most common sightings of this error show up while working with Django, HTTP client libraries, and JSON data. In each case, it helps to look closely at what the library returns before choosing a remedy.
- Django Querysets And Forms — A queryset might be a list like object, while
.values()yields dictionaries. Callingget()directly on a queryset differs from calling.get()on a dictionary. Check the documentation for each method so you match the call to the return type. - Requests And JSON Responses — When you call
response.json(), the result may be a dictionary at the top level or a list at the top level, depending on the API. Code that assumes a dictionary and callsdata.get("field")will fail on list responses. - REST Serializers — Serializers can return one object or many objects. The many case often comes back as a list of dictionaries. Use a loop to call
get()on each dictionary, or adjust the view to always wrap the data in a dictionary with a fixed field name. - Third Party SDKs — Cloud client libraries and SDKs sometimes return wrapper objects that behave like lists but do not carry all dictionary methods. Reading small slices and printing
type()clarifies what you are handling.
In all of these settings, steady habits make life easier. Read return type sections in the docs, keep an eye out for “list of” versus “mapping of” wording, and add type hints in your own code so mismatches show up in static checks before they reach production.
Preventing This Error In New Code
Once you have cleaned up the current traceback, a few small habits cut down on repeat visits from this AttributeError. These changes fit well into both small scripts and large applications.
- Add Type Hints — Use annotations such as
List[str]andDict[str, Any]. Tools like mypy or pyright will warn when you callget()on a variable that may be a list. - Name Variables For Their Shape — Pick names like
user_list,user_dict,items_by_id, orrows. That makes it easier to remember which methods fit each object. - Write Small Tests Around Data Shape — Unit tests can assert that helper functions return dictionaries or lists. When someone changes an implementation in a way that flips the type, tests fail quickly instead of leaving surprises for run time.
- Prefer Safer Access Patterns — When you read data from external sources, guard it with checks such as
isinstance(obj, dict)orisinstance(obj, list)before calling methods likeget(). - Log Types In Error Handlers — Where you already handle exceptions, add logging that prints both the value and its type. That shortens the next debugging session when a similar issue pops up.
These habits build a code base where the message AttributeError: ‘List’ Object Has No Attribute ‘Get’ becomes rare. When it does appear, you can trace it quickly to a mismatch between returned data and expected methods, then apply a targeted fix.
