Skip to main content

Posts

Showing posts with the label not str in Python Solve TypeError: list indices must be integers or slices

Solve TypeError: list indices must be integers or slices, not str in Python Gorakh Gupta The Crazy Programmer

In python, we can have many types of errors in our code. One of the most common errors is TypeError: list indices must be integers or slices, not str. In this article, we are going to see what causes this error and how can we fix this error. This error mostly comes when we are working with lists in python. Whether we are printing the contents of a list or modifying any value at a particular position in the list. In this article, we will discuss some points to remember when working with lists so that the probability of occurring these types of errors will be much less. Case 1: # Declare a List l1 = [10, 20, 30, 40, 50] # Get Index input from User index = input("Enter the Index at which you want to change value: ") # Get the number from user number = int(input("Enter the final value you want in list : ")) # Update the value in list l1[index] = number # Print the list after updation print(f"Updated list is : {l1}") Output: PS C : \ Users \ ASUS \ Des