FIX – TypeError: list indices must be integers or slices not float

FIX - TypeError list indices must be integers or slices not float

A list in Python is used to store a collection of values. Each value in the list can be accessed using its index which shows its position in the list. For example, In a list, the first element’s index is 0 and the second element’s index is 1, and so on. In this article, we will understand the topic and we will discuss different solutions to fix it.

“TypeError: list indices must be integers or slices not float” error occurs when trying to access a list using a float value as an index. Lists can only be indexed with integers or slices. The solution is to use integer values as indices or convert the float to an integer before accessing the list.

Note: Python is zero-indexed, which means the first element has an index of 0, the second has an index of 1, and so on

TypeError: list indices must be integers or slices not float

Why does the list indices must be integers or slices, not float error occurs?

If we read the list indices must be integers or slices, not float error messages, it itself explains its meaning. It occurs when we try to access the elements of the list using a Floating point. Whereas, the indexes are integers. To access an item or element in a list using its index, we should provide an integer value. This is because indices are used to indicate a specific position in the list, and floating points do not make sense in this context. 

For example, let’s say you have a list of numbers:

myList = [11, 12, 13, 14, 15]
myList[1.0]

Output

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

We can observe when we tried to use a float value as an index, the list indices must be integers or slices, not float error message was raised. 

Have a look at another example that can cause this error. Let’s try to access the element that is in the middle of the integers list. 

myList = [11, 12, 13, 14, 15]
middle_index = len(myList) / 2
middle_item = myList[middle_index]

Output

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

On execution of this code, the list indices must be integers or slices not float error is raised because the middle_index that we calculated is a floating-point number, not an integer. Because the division operator (/) in Python is used to return a floating-point number, which cannot be used to index a list. Check the below section to see its solution.

How to fix the list indices must be integers or slices, not float error?

To fix the list indices must be integers or slices, not float error in Python, we need to make sure that we are using integer values as list indices.

Use Integer Index

In Python, use integer values as list indices to avoid this error. 

myList = [11, 12, 13, 14, 15]
print(myList[1])

Output

12

In this example, the integer value 1 is used as an index to get the second element of a list.

Note: We can convert the float value to an integer using the int() function as shown below:

myList = [11, 12, 13, 14, 15]
index = 1.5
print(myList[int(index)])

Output

12

In the above example, the int() method is used to convert the float value to an integer to access the second element in the list.

Use Slicing

To extract a range of values from the list, use slicing if you want to work with non-integer values. 

Suppose there is a float value index and we want to extract a portion of the list myList starting from this index, we can do this:

myList = [11, 12, 13, 14, 15]
index = 1.5
my_slice = myList[int(index):]
print(my_slice)

Output

[12, 13, 14, 15]

The above code snippet converted the float value to an integer and use it as the starting index for the slice. The slice extracted all the elements in the list from this index to the end of the list.

Use Operator // instead of /

To fix the list indices must be integers or slices, not float error, use integer division instead of regular division by using the floor division operator (//) instead of the division operator (/):

myList = [11, 12, 13, 14, 15]
middle_index = len(myList) // 2
middle_element = myList[middle_index]
print(middle_element)

Output

13

We can observe, now we can access the middle element of the list without any errors.

Conclusion

In conclusion, the list indices must be integers or slices not float error occurs when a floating-point number is used to index a list in Python. This error is commonly caused by using the division operator (/) to calculate an index, which returns a floating-point number. To fix this error, it is important to use only integers or slices when indexing a list. The floor division operator (//) can be used to ensure that a calculated index is an integer and not a floating-point number. By understanding the causes of this error and implementing the appropriate solutions, you can ensure that your code runs smoothly and without errors when working with lists in Python.

Read More:

How to Modify Python Global Variable inside a Function

How to Convert JSON to YAML

Fix – attributeerror: module numpy has no attribute int

How to Send SMS Text Messages Using Python

Coding Spell Python Journey | About Us

Latest posts by Muhammad Usman Shafique (see all)