How To Solve “TypeError: list indices must be integers or slices not float” in Python

TypeError: list indices must be integers or slices not float

There are many causes of “TypeError: list indices must be integers or slices not float”, such as using float to access the list and using division “/” to calculate the index. In this article, we will give solutions to overcome such as using “int()” function to convert to int data type.

When does the “TypeError: list indices must be integers or slices not float” occur?

This error occurs when using float data type to access the list. Here are some examples:

Use the value of the list to access the list.
This error often occurs with Python beginners when confusing the value in the list with the index of the list. The reason is because of not understanding “for loop” in Python.

Example:

priceList = [199.85, 299.52, 399.42]

for price in priceList:
  	# Use the value to access the list.
	salePrice = priceList[price] - 50
	print(salePrice)

Output:

TypeError: list indices must be integers or slices, not float

Don’t convert float to int.
The error also occurrs due to using float data type to access the list.

Example:

fruits = ["apple", "banana", "cherry"]
index = 1.0

# Using float data type to access the list
print(fruits[index])

Output:

TypeError: list indices must be integers or slices, not float

Use division “/” to calculate the index.
This division will return float data type, causing the program to crash.

Example:

fruits = ["apple", "banana", "cherry"]

# This division will return float data type
index = 2 / 1
print(fruits[index])

Output:

TypeError: list indices must be integers or slices, not float

Solution for the “TypeError: list indices must be integers or slices not float”

Do not use the value of the list as an index

Instead of looping based on value, we loop based on the index range of the list. In the example below, we use the “len()” function to measure the length of the string and use the “range()” function to make the for loop run in the range of the list.

Example:

priceList = [199.85, 299.55, 399.42]

# Use index in for loop
for price in range(len(priceList)):
	salePrice = priceList[price] - 50
	print(salePrice)

Output:

149.85
249.55
349.42

Convert float to integer

To avoid errors, check the data type before using it. In this case, we convert float to int. We use the “int()” function.

Example:

fruits = ["apple", "banana", "cherry"]
index = 1.0

# Use "int()" convert float to int
print(fruits[int(index)])

Output:

banana

Use floor division “//”

If you want to get integer data type after division, then you should use floor division “//”.

Example:

fruits = ["apple", "banana", "cherry"]

# Use floor division "//" to get int
index = 2 // 1
print(fruits[index])

Output:

Cherry

Summary

Through this article, we have a better understanding of the TypeError: list indices must be integers or slices not float. The most common cause is using a non-int data type to access the list. We need to make sure to convert to int before accessing the list. Hopefully, through this article, you will fix the error quickly.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *