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
Passing
[{"id":9,"name":"Foo"}]
instead of['{"id":9,"name":"Foo"}']
which can done by removingjson_encode()
while sending data to Flask Application. In my case was from Laravel / PHP.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
Pandas: Merge Data Frames on Datetime Index
How to Check the Version of Python Modules
Finding Length of the Longest List in an Irregular List of Lists
How to Read Gz Compressed File by Pyspark
Optimal Way to Store Data from Pandas to Snowflake
Pick Dictionary Keys:Values Randomly
Check to See If Python Script Is Running
Pandas Open_Excel() Fails With Xlrd.Biffh.Xlrderror: Can't Find Workbook in Ole2 Compound Document
Sum of Square Differences (Ssd) in Numpy/Scipy
Pip Install Not Working With Jenkins
How to Open Excel File Fast in Python
Conda: Remove All Installed Packages from Base/Root Environment
Using a Global Variable With a Thread
How to Convert Column With Dtype as Object to String in Pandas Dataframe
How to Enable Autocomplete (Intellisense) for Python Package Modules