← Back

Home › Errors › TypeError

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

The fix, step by step

  1. Print type(container) — confirm it is really a list and not the dict you assumed.
  2. If it is a list of records, subscript twice: first a position, then the key — rows[0]["name"].
  3. If you need name-based access, build the dict you wanted: prices = {item["name"]: item["price"] for item in items}.
  4. If the loop was the problem, iterate the list directly — for item in items: — instead of generating indices or keys.

Related messages worth knowing

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।

पूरी Hindi explanation पढ़ें →