Python3: Typeerror: List Indices Must Be Integers or Slices, Not Str

Python3 TypeError: list indices must be integers or slices, not str

First of all don't name your variables after built in python statements or data structures like list, tuple or even the name of a module you import, this also applies to files. for example naming your file socket.py and importing the socket module is definitely going to lead to an error (I'll leave you to try that out by yourself)

in your code element is a string, indexes of an iterable must be numbers not strings, so you can tell python

give me the item at position 2.

but right now you're trying to say give me the item at position A and that's not even valid in English, talk-less of a programming language.

you should use the enumerate function if you want to get indexes of an iterable as you loop through it or you could just do

for i in range(len(list))

and loop through the range of the length of the list, you don't really need the elements anyway.

Here is a simpler approach to what you want to do

s = string = 'AAAABBBCCDAABBB'

ls = []

for i in s:
if ls:
if i != ls[-1]:
ls.append(i)
else:
ls.append(i)

print(ls)

How to solve type error: "list indices must be integers or slices, not str" in a for loop

list1 is a list of strings. This is because the numbers have quotes - " " around them.

In your for loop, you are looping over these strings, each iteration of the loop i is set to a string, "1", then "2"...

When you call pop(i) you are passing a string to it, but you need to pass an integer.

It can be helpful to print what is going on your program, for example in your loop you can

print(i, type(i))

to help keep track of what is going on.

Not sure what your end goal is with this, but there are nice clean ways to keep track of what you are lopping over.

for i, element in enumerate(list):

will give you access to both the current element, and the current iteration at the same time.

Using this in your example could look like:

list1 = ["1", "2", "4", "5", "3"]
for i, element in enumerate(list):
if element == "3":
list.pop(i)
break

TypeError: list indices must be integers or slices, not str - While accessing element of list

I was solve this by the following

  1. Passing [{"id":9,"name":"Foo"}] instead of ['{"id":9,"name":"Foo"}'] which can done by removing json_encode() while sending data to Flask Application. In my case was from Laravel / PHP.

  2. By accessing element using a[0]['id'] as pointed by @mhhabib

Python Error : list indices must be integers or slices, not str

You are declaring a list here mydir = []

Change it to dictionary mydir = {}

User-given input produces TypeError: list indices must be integers or slices, not str

input() returns a string containing whatever the user entered; you need to explicitly transform it into an integer:

ans = input("Enter the number to modify :")

should be

ans = int(input("Enter the number to modify :"))


Related Topics



Leave a reply



Submit