🔧 Error Fixes
· 1 min read

Python TypeError: 'NoneType' Object Is Not Subscriptable — How to Fix It


TypeError: 'NoneType' object is not subscriptable

You’re using [] on a value that’s None. A function probably returned None instead of a list or dict.

Fix 1: Check the return value

# ❌ .append() returns None
items = [1, 2, 3].append(4)
print(items[0])  # TypeError! items is None

# ✅ append modifies in place
items = [1, 2, 3]
items.append(4)
print(items[0])  # 1

Other methods that return None: .sort(), .reverse(), .extend(), .insert().

Fix 2: Function returns None

def find_user(name):
    for user in users:
        if user["name"] == name:
            return user
    # No return = returns None!

# ❌ If user not found, result is None
result = find_user("Bob")
print(result["email"])  # TypeError!

# ✅ Check first
result = find_user("Bob")
if result:
    print(result["email"])

Fix 3: Dictionary .get() returns None

data = {"users": [{"name": "Alice"}]}

# ❌ .get() returns None if key missing
items = data.get("posts")
first = items[0]  # TypeError!

# ✅ Provide default
items = data.get("posts", [])
first = items[0] if items else None
📘