Python List Indices Must Be Integers Not Str

This error occurs as soon as utilizing a string for list indexing rather of indices or slices. For a much better expertise of list indexing, view the image below, which mirrors an instance list labeled through the index values:

You watching: Python list indices must be integers not str


Using indices refers to the usage of an integer to rerevolve a certain list worth. For an instance, watch the following code, which returns the item in the list with an index of 4:

Using slices suggests specifying a combination of integers that pinallude the start-suggest, end-point and action dimension, returning a sub-list of the original list. See below for a quick demonstration of making use of slices for indexing. The initially example offers a start-point and end-point, the second one introduces a step-size (note that if no step-dimension is defined, 1 is the default value):

value_list_1 = example_list<4:7> # returns indexes 4, 5 and also 6 <8, 1, 4>value_list_2 = example_list<1:7:2> # retransforms indexes 1, 3 and 5 <7, 0, 1>

See more: ' The Last Book Party Review By Kristy Woodson Harvey: The Last Book Party

As a budding Pythoneer, among the many practical devices in your belt will certainly be list indexing. Whether making use of indices to rerevolve a particular list worth or making use of slices to rerevolve a variety of worths, you"ll discover that having solid skills in this location will be a boon for any type of of your present or future projects.

Today we"ll be looking at some of the problems you might enrespond to once using list indexing. Specifically, we"ll emphasis on the error TypeError: list indices should be integers or slices, not str, with some valuable examples of where this error could take place and also just how to resolve it.

For a quick example of how this might happen, take into consideration the situation you wanted to list your optimal 3 video games franchises. In this example, let"s go with Cevery one of Duty, Final Fantasy and Mass Effect. We have the right to then add an input to the manuscript, enabling the user to pick a list index and then print the corresponding worth. See the complying with code for an example of exactly how we deserve to perform this:

favourite_three_franchises = <"Call of Duty", "Final Fantasy", "Mass Effect">choice = input("Which list index would you like to pick (0, 1, or 2)? ")print(favourite_three_franchises)