Typeerror list indices must be integers or slices not float

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:

2

2. 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:

5

3. 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:

shoes

4. 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:

bags

Conclusion

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.