Loading...
Loading...
TypeError: 'NoneType' object is not subscriptableYou're trying to use [] indexing on a value that is None. This happens when a function returns None (either explicitly or by not having a return statement) and you try to index into the result.
Methods like .sort(), .reverse(), .append() modify the list in-place and return None.
# ❌ sort() returns None
my_list = [3, 1, 2]
my_list = my_list.sort() # my_list is now None!
print(my_list[0]) # 💥 TypeError
# ✅ Sort in -place without reassigning
my_list.sort()
print(my_list[0]) # ✅
# ✅ Or use sorted() which returns a new list
my_list = sorted([3, 1, 2])
print(my_list[0]) # ✅Check that the value is not None before using it.
result = get_data()
if result is not None:
print(result[0])
else:
print("No data returned")