close

[Solved] Why am I seeing “TypeError: string indices must be integers”?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Why am I seeing “TypeError: string indices must be integers”? in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Why am I seeing “TypeError: string indices must be integers” Error Occurs?

Today I get the following error Why am I seeing “TypeError: string indices must be integers”? in python.

How To Solve Why am I seeing “TypeError: string indices must be integers” Error ?

  1. How To Solve Why am I seeing “TypeError: string indices must be integers” Error ?

    To Solve Why am I seeing “TypeError: string indices must be integers” Error item is most likely a string in your code; the string indices are the ones in the square brackets, e.g., gravatar_id.

  2. Why am I seeing “TypeError: string indices must be integers”?

    To Solve Why am I seeing “TypeError: string indices must be integers” Error item is most likely a string in your code; the string indices are the ones in the square brackets, e.g., gravatar_id.

Solution 1

I had a similar issue with Pandas, you need to use the iterrows() function to iterate through a Pandas dataset Pandas documentation for iterrows

data = pd.read_csv('foo.csv')
for index,item in data.iterrows():
    print('{} {}'.format(item["gravatar_id"], item["position"]))

note that you need to handle the index in the dataset that is also returned by the function.

Solution 2

item is most likely a string in your code; the string indices are the ones in the square brackets, e.g., gravatar_id. So I’d first check your data variable to see what you received there; I guess that data is a list of strings (or at least a list containing at least one string) while it should be a list of dictionaries.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read