What this error means
You used square brackets [] on a function instead of calling it with parentheses (). Functions need to be called first to get a result, then you can index that result.
Common causes
- You forgot parentheses when calling a function.
- You used
[]instead of()on a function. - Confusion between function calls and indexing.
Example that causes the error
items = [1, 2, 3]
first = len[0] # TypeError: should be len(items)[0] or items[0]
result = sorted[0] # TypeError: should call sorted() first
How to fix it: call the function first
Call the function with (), then index the result:
items = [1, 2, 3]
length = len(items) # OK: call function first
# Or if you want first item:
first = items[0] # OK: index the list directly
Index the result, not the function
data = [3, 1, 2]
sorted_data = sorted(data) # Call function first
first = sorted_data[0] # OK: index the result
# Or in one line:
first = sorted(data)[0] # OK: call, then index
Remember: () for functions, [] for indexing
# Functions use parentheses:
result = some_function() # OK
# Then index the result:
value = result[0] # OK
# Don't mix them up:
value = some_function[0] # TypeError!