In Python, we can use index numbers to access an individual element from a list object. The index number of every list object starts from 0 to n-1, where n is the total number of elements present in the list. While accessing a single element using indexing, we use the associated integer value as an index number. If we try to use a float number as an index value, we encounter an error.
TypeError: list indices must be integers or slices, not float
.
In this Python error guide, we will discuss this error in detail and learn why it occurs and how to solve it. We will also walk through a common example that demonstrates this error in a Python program. So let's get started with the error statement.
Python Problem: TypeError: list indices must be integers or slices, not float
Python list stores its elements in sequential order, and we can use the index numbers to access an individual or a sequence of elements from the list. The index numbers are the integer value from 0 to n-1. Where 0 represents the index number for the first element and n-1 represents the last element's index number.
In Python, apart from integers, we also have floating-point values to represent numerical data, but if we pass the floating-point number as an index number, Python raises the
TypeError: list indices must be integers or slices, not float
Error. The Error statement "
TypeError: list indices must be integers or slices, not float
" has two parts.
- TypeError (Exception Type)
- list indices must be integers or slices, not float (Error Message)
1. TypeError
TypeError is one of Python's standard exceptions. It is raised in a Python program when we perform an unsupported operation or function on an inappropriate type. This error also occurs when we pass an invalid data type argument to a method or function. And when we pass a float number instead of an integer as an index, Python raises this Exception.
2. list indices must be integers or slices, not float
list indices must be integers or slices, not float
is the Error message, telling us that the index needs to be an integer value or a slice syntax, not float. This error message is raised in a Python program when we pass a floating-point number inside the square bracket to access the list element.
Example
# list
my_list = [10, 20, 30, 40, 50]
# float number
index = 2.0
print(my_list[index])
Output
Traceback (most recent call last):
File "main.py", line 8, in
print(my_list[index])
TypeError: list indices must be integers or slices, not float
In the above example, we are getting this error because we are passing a float number
2.0
as an index value to the list
my_list
. And the Python list does not accept floating-point numbers as an index value.
Common Example Scenario
Let's say we have a list top3 that contains the information of the top 3 students from a class, and we need to create a program that accepts a number between 0 to 2 and returns the particular student's information.
Example
top3= [
['1','Rahul', '990', '17'],
['2','Ravi', '987', '17'],
['3','Anil', '967', '17'],
]
# convert the input number in float
rank = float(input("Enter a Number between 0 to 2: "))
print("Rank:", top3[rank][0])
print("Name:", top3[rank][1])
print("Marks", top3[rank][2])
print("Age", top3[rank][3])
Output
Enter a Number between 0 to 2: 1
Traceback (most recent call last):
File "main.py", line 10, in <module>
print("Rank:", top3[rank][0])
TypeError: list indices must be integers or slices, not float
Break the code
In the above example, we are getting the error in
line 10
with
print("Rank:", top3[rank][0])
statement. This is because the value of
rank
in that line is
1.0
which is a floating-point number. While accepting the input from the user, we are converting it into a float with the
float()
function, and using that float value to access the
top3
list items.
Solution
If we accept a number from the user side as an index value, we always convert that user input into an integer number using the Python int() function.
top3= [
['1','Rahul', '990', '17'],
['2','Ravi', '987', '17'],
['3','Anil', '967', '17'],
]
# convert the input number in integer
rank = int(input("Enter a Number between 0 to 2: "))
print("Rank:", top3[rank][0])
print("Name:", top3[rank][1])
print("Marks", top3[rank][2])
print("Age", top3[rank][3])
Output
Enter a Number between 0 to 2: 1
Rank: 2
Name: Ravi
Marks 987
Age 17
Wrapping Up!
The " TypeError: list indices must be integers or slices, not float " is a common error. You will only encounter it in your Python program when you use a float number instead of an integer as an index value. Converting a number into a float and using it as an index value is a common case when many Python developers encounter this error.
Even when slicing a list, rather than using a colon
:
to separate the start index and end index, if you use the dot, there also you will get the same error.
If you are still getting this error in your Python program, you can share your code and query in the comment section. We will try to help you in debugging.
People are also reading:
- Python TypeError: ‘function’ object is not subscriptable Solution
- How do you remove duplicates from a Python list while preserving order?
- Python List Methods: All you need to Know
- Replace Item in Python List
- Python typeerror: ‘int’ object is not subscriptable Solution
- Difference between Python classmethod and staticmethod
- Python NameError name is not defined Solution
- How to Get Open Port Banner in Python?
- Python IndexError: tuple index out of range Solution
- Python SyntaxError: positional argument follows keyword argument Solution
Leave a Comment on this Post