The “typeerror list indices must be integers or slices not float” is an error message in Python.
If you encounter this error and don’t know how to resolve it?
And so continue exploring this article.
In this article, we’ll show you how to fix the “list indices must be integers or slices not float” in the Python typeerror.
Aside from giving you the solutions, we’ll also delve into what this error means and why it occurs in your code.
What is “list indices”?
A list in Python is an ordered collection of items, and each item has an index.
The index is used in accessing and manipulating specific items in the list.
There are two types of indices in Python, such as:
✔ Integer indices
Integer indices are used to access a single item in a list.
✔ Slice indices
Slice indices are used to access a range of items in a list.
What is “typeerror list indices must be integers or slices not float”?
The “typeerror list indices must be integers or slices, not float” occurs when you are trying to access an element of a list using a float (a number with a decimal point (1.5)) as the index.
List indices must be integers (whole numbers) or slices (a range of integers).
And using a floating-point number as an index will result in a “TypeError” in Python.
Why does the “list indices must be integers or slices not float” error message occurs?
This error can occur due to some reasons, such as:
❌ When you mistakenly use a floating-point number as an index when trying to access an element in a list.
count = [1, 2, 3, 4, 5]
avg = sum(count) / len(count)
# accidentally using a float index
result = count[avg]If you try to run this code it will result to:
TypeError: list indices must be integers or slices, not float❌ When you are using a variable that contains a float as an index.
index = 2.5
result = numbers[index]❌ When you use the division operator (/) to access a list item.
item = ['phones', 'shoes', 'bags']
index = 2 / 1
print(index)
print(item[index])How to fix “typeerror list indices must be integers or slices not float”?
To fix this error, make that you are using integer or slice values as indices for lists.
If you need to use a floating-point number to access an element.
You have to convert the float to an integer or slice using the int() or slice() functions to access the desired element of the list.
Here is the list of solutions you can use to solve the error:
1. Convert the float to an integer using the int() function
One solution is to convert the float to an integer using the int() function.
This will convert the float into an integer, which can be used as a list index.
count = [1, 2, 3, 4, 5]
index = 1.5
# convert float to int
index = int(index)
result = count[index]
print(result)
Output:
22. Round the float to the nearest integer using the round() function
To round the float to the nearest integer using the round() function, it will return an integer.
count = [1, 2, 3, 4, 5]
index = 4.5
# round float to nearest integer
index = round(index)
result = count[index]
print(result)Output:
53. Use math.floor() or math.ceil() functions to round down or up
You can use math.floor() and math.ceil() functions from the math module to round down or up the float index before using it to access an element of an item.
import math
item = ['phone', 'bags', 'shoes']
index = 1.6
result = item[math.floor(index)]
print(result)
Output:
bags
Example code using math.ceil() function:
import math
item = ['phone', 'bags', 'shoes']
index = 1.6
result = item[math.ceil(index)]
print(result)Output:
shoes4. Use slicing to access a range of elements
You can also use slicing to access a range of elements in an item.
The start and end indices are converted to integers using the int() function before being used in the slice.
items = ['phone', 'bags', 'shoes']
start_index = 0.0
end_index = 3.0
result = items[int(start_index):int(end_index)]
print(result)
Output:
['phone', 'bags', 'shoes']5. Use conditional statement to check if the index is a float and handle it accordingly
Alternatively, you can use a conditional statement to check if an index is a float.
If it is, it is converted to an integer using the int() function before being used to access an element of an item.
items = ['phone', 'bags', 'shoes']
index = 1.0
if isinstance(index, float):
index = int(index)
result = items[index]
print(result)
Output:
bagsConclusion
The “typeerror list indices must be integers or slices not float” in an error message in Python.
That usually occurs when you are trying to access an element of a list using a float (a number with a decimal point (1.5)) as the index.
This article already provides different solutions above so you can fix the error message immediately.
We are hoping that this article provided you with sufficient solutions to get rid of the error.
You could also check out other “typeerror” articles that may help you in the future if you encounter them.
- Typeerror data map is not a function
- Typeerror: fsevents.watch is not a function
- Typeerror: res.json is not a function
Frequently Asked Questions
What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
How do I prevent TypeError in production?
Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.
Where can I find more TypeError fixes?
Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.
