Python TypeError

Python TypeError: list indices must be integers or slices, not str — Causes & Fix

Python TypeError: list indices must be integers — users[name] fix

What Does This Error Mean?

Python raises TypeError: list indices must be integers or slices, not str when you try to access a list element using a string key instead of an integer index. Lists are ordered sequences indexed by position (0, 1, 2…). String keys are for dictionaries.

Common Causes (With Code)

1. Treating a list like a dictionary

❌ Causes the error

users = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]

print(users["name"])
# TypeError: list indices must be integers or slices, not str

2. Parsing JSON — confusing a list with a dict at the wrong nesting level

❌ Causes the error

import json

response = '[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]'
data = json.loads(response)     # data is a LIST

print(data["name"])   # expecting dict behaviour on a list
# TypeError: list indices must be integers or slices, not str

3. Using a float as the index

❌ Causes the error

items = [10, 20, 30]
i = 1.0                  # float
print(items[i])
# TypeError: list indices must be integers or slices, not float

4. Looping over a list of dicts — missing the iteration step

❌ Causes the error

products = [{"id": 1, "price": 9.99}, {"id": 2, "price": 4.99}]

# Accessing the list directly instead of iterating
print(products["price"])
# TypeError: list indices must be integers or slices, not str

How to Fix It

Fix 1 — Index the list with an integer, then access the dict key

✅ Correct

users = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]

print(users[0]["name"])    # Alice
print(users[1]["name"])    # Bob

Fix 2 — Iterate with a for loop to access each dict

✅ Correct

users = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]

for user in users:
    print(user["name"])    # Alice, then Bob

Fix 3 — Check the JSON structure before accessing keys

✅ Correct

import json

response = '[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]'
data = json.loads(response)   # list of dicts

# Iterate — each element IS a dict
for item in data:
    print(item["name"])   # Alice, Bob

Fix 4 — Convert float index to int

✅ Correct

items = [10, 20, 30]
i = 1.0
print(items[int(i)])   # 20  ✅

Fix 5 — Use type() to inspect the data structure before indexing

✅ Diagnostic tip

data = some_function()
print(type(data))      #  or ?
print(data)            # inspect contents

# If it's a list:  data[0]["key"]
# If it's a dict:  data["key"]

Frequently Asked Questions

What's the difference between a list and a dict in Python?

A list ([]) is an ordered sequence accessed by integer position (data[0]). A dict ({}) is a key-value mapping accessed by key (data["name"]). JSON arrays map to lists; JSON objects map to dicts.

Can I use a string to search inside a list?

Not with bracket notation — strings are not valid list indices. Use in for membership testing or a list comprehension to filter by value:

fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)                # True
found = [f for f in fruits if "an" in f] # ['banana']