TypeError: list indices must be integers or slices, not str — decoded
Exception: TypeErrorCategory: runtimeRaised at: when you subscript a list with a string
What this message means
A list is an ordered sequence: its only addressing scheme is a position — items[0], items[-1], items[1:4]. A string inside the brackets is a *key*, and keys belong to dictionaries, not lists. When Python sees prices["apple"] and prices is a list, it raises this TypeError with the exact rule you broke.
The two usual stories behind it: the data really is a list and you should use a position or a loop — or the data should have been a dict and somewhere along the way you (or a JSON response) built the wrong shape.
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(prices["apple"])
~~~~~~^^^^^^^^^^
TypeError: list indices must be integers or slices, not str
A real example
prices = [40, 55, 30] # a list — positions only
print(prices["apple"]) # a key — dicts only
The fix depends on which shape you *meant*. If the names matter, build a dict. If the order is enough, use the index:
prices = {"apple": 40, "banana": 55, "cherry": 30}
print(prices["apple"]) # 40
# or keep the list and use positions
print(prices_list[0]) # 40
The JSON edition of this bug
JSON objects become dicts and JSON arrays become lists — so the same URL can hand you either shape on a different day. data["items"][0]["name"] is correct when items is an array of objects, but fails with this error the moment items is a list and you skip the [0]:
data = {"items": [{"name": "pen"}, {"name": "ink"}]}
print(data["items"]["name"]) # TypeError — items is a LIST
print(data["items"][0]["name"]) # pen — subscript the list first
When a request returns something unexpected, print the type at each level — type(data), type(data["items"]) — until the shape in your head matches the shape in memory.
The causes behind almost every case
- List of dicts mistaken for a dict of dicts.
rows["id"]fails;rows[0]["id"]is what a list of records needs. - Iterating keys over a list.
for key in keys: total += prices[key]is fine only whenpricesis a dict — inside a loop it is easy to lose track. - A variable name that lies.
menu["tea"]reads naturally, so the mistake hides — until you check andmenuis a list of names, not a mapping. - Converting between shapes. Turning a dict into
list(d)gives you its *keys* as a list; indexing that with a string then fails.
The fix, step by step
- Print
type(container)— confirm it is really alistand not the dict you assumed. - If it is a list of records, subscript twice: first a position, then the key —
rows[0]["name"]. - If you need name-based access, build the dict you wanted:
prices = {item["name"]: item["price"] for item in items}. - If the loop was the problem, iterate the list directly —
for item in items:— instead of generating indices or keys.
Related messages worth knowing
- TypeError: the parent guide — the parent explainer for every wording of this exception.
- KeyError — the mirror mistake on the dict side — a key that does not exist.
- IndexError: list index out of range — the sibling message when the index is valid type but the position does not exist.
Fix it interactively
These free problems practice choosing between positions and keys — the exact decision this error is about:
Practice more Python bugs →🇮🇳 Hindi में समझें
List में सिर्फ position से पूछते हैं (items[0]), और dict में सिर्फ key से (prices["apple"])। यह error तब आता है जब list पर key से पूछ बैठते हो। JSON में array → list और object → dict बनता है, इसलिए data["items"]["name"] गलत है और data["items"][0]["name"] सही। पहला कदम हमेशा: print(type(x)) करके देखो कि data असल में क्या है — फिर तय करो कि position चाहिए या key।