Lists are indexed using index numbers. These numbers are integer values. If you try to access an item from a list using a floating-point number, the “TypeError: list indices must be integers or slices, not float” error will be raised.
This guide discusses what this error means and why you may encounter it. We’ll walk through an example of this error so you can learn how to fix it.
TypeError: list indices must be integers or slices, not float
When an item is added to a list, the item is assigned an index value. Index values start from zero and increment by one for each new item in the list. This makes it easy to access individual items. The first item in a list has the index 0, the second has the index 1, and so on.
You cannot retrieve items from a list using floating-point numbers. Floating-point numbers are a different data type. As a result, they are treated differently by Python. Lists expect an integer index number because lists are indexed using integers.
An Example Scenario
We’re going to build a program that retrieves information about a participant in a tennis tournament. To start, define a list of lists. This list of lists will contain information about all of the participants in the tournament:
participants = [ ["Alex Rogers", 5, 2], ["Linda Patterson", 3, 4], ["Ruby Spencer", 1, 6] ]
Our list of participants contains three lists. Each of these lists the name of a participant, the number of games they have won, and the number of games they have lost, in that order.
The values are ordered by the number of times a player has won.
Next, we ask the user for the player whose information they want to view. We do this using an input() statement:
player_to_find = float(input("Enter the leaderboard position of the player you want to find: "))
The number a user inserts should correspond to the position of a player on the leaderboard. Alex Rogers is first. To find out about him, the user would insert 1.
We convert the value the user inserts to a float because we’re going to be using indexing later in our code. Index values are numerical and so we’ll need a number to work with.
Next, use indexing to retrieve the player for which the user is looking:
player_info = participants[player_to_find - 1]
We use the value the user inserts to find the record of an individual player from our “participants” list. We subtract 1 to the value of “player_to_find” because lists are indexed from zero and the first person on the leaderboard will be in position #1.
Next, print out the details about this participant to the console:
print("Name: {}".format(player_info[0])) print("Wins: {}".format(player_info[1])) print("Losses: {}".format(player_info[2]))
We display the name of a player, how many games they have won, and how many games they have lost to the console. Let’s run our program:
Enter the leaderboard position of the player you want to find: 2 Traceback (most recent call last): File "main.py", line 9, in <module> player_info = participants[player_to_find + 1] TypeError: list indices must be integers or slices, not float
After we insert a leaderboard position into our program, an error is raised.
The Solution
We convert the value a user inserts into our program to a float. We do this using the float()
method.
This is a mistake because when it comes time to retrieve an item from our “participants” list, we try to do so with a floating-point number. This causes an error.
To fix our program, we must convert the value a user inserts to an integer instead of a floating-point number. We can do this using the int()
method:
player_to_find = int(input("Enter the leaderboard position of the player you want to find: "))
Now that we have an integer value, we can retrieve items from our lists using indexing. Run our code and see what happens:
Enter the number of the player you want to find: 2 Name: Linda Patterson Wins: 3 Losses: 4
Our code successfully shows us the player record of the individual who is at position #2 on the leaderboard.
Conclusion
The “TypeError: list indices must be integers or slices, not float” error occurs when you try to access an item from a list using a floating-point number. To solve this error, make sure you only use integers to access items in a list by their index value.
Now you’re ready to fix this error in your code like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.