Dictionary with nested list TypeError: string indices must be integers

string indices must be integers json dictionary
python for loop string indices must be integers
typeerror: string indices must be integers json
python dictionary string indices must be integers
string indices must be integers postgres
python sorted string indices must be integers
list indices must be integers or slices, not str json
typeerror: string indices must be integers nested dictionary

My json response come back with this dictionary.

data = {"offset": 0, "per-page": 1, "total": 548, "language": "en", 
"odds-type": "DECIMAL", "overall-staked-amount": 23428.63548, 
"profit-and-loss": 4439.61471, "events": [{"id": 1042867904480016, 
"name": "Gael Monfils vs Daniil Medvedev", "sport-id": 9, "sport- 
url": "tennis", "sport-name": "Tennis", "start-time": "2019-02- 
16T14:29:00.000Z", "finished-dead-heat": false, "markets": [{"id": 
1042867905130015, "name": "Moneyline", "commission": 0, "net-win- 
commission": 0, "profit-and-loss": -0.59999, "stake": 0.59999, 
"selections": [{"id": "1042867905220015_BACK", "runner-id": 
1042867905220015, "name": "Daniil Medvedev", "side": "BACK", "odds": 
3.0, "stake": 0.59999, "commission": 0, "profit-and-loss": -0.59999, 
"bets": [{"id": 1043769075060320, "offer-id": 1043764555430020, 
"matched-time": "2019-02-16T16:16:18.936Z", "settled-time": "2019- 
02-16T16:26:01.878Z", "in-play": true, "odds": 3.0, "stake": 
0.59999, "commission": 0, "commission-rate": 2.0, "profit-and-loss": 
-0.59999, "status": "PAID"}]}], "net-win-commission-rate": 0.02}]}]}

I am unable to get the attribute value for overall-staked-amount and inside the events list I cannot get name from events list. using list comprehension or a for loop.

Here's my code.

list comp

overall_staked = [d['overall-staked-amount'] for d in data]

print(overall_staked)

for loop

for d in data:
    overall_staked = d['overall-staked-amount']
    name = d['name']
    print(overall_staked,name)

I receive an error TypeError: string indices must be integers what am I doing wrong or need to do?

No need to iterate, just do:

overall_staked = data['overall-staked-amount']

Follow the same logic to get other data

TypeError: string indices must be integers, not dict, Based on the sample you gave, it looks like you are looking for something a little like this. def FunDictr(base_dictionary,string_key): d = dict() d[string_key] = dict() � The problem I'm having is when attempting to loop round the Direction list and have corresponding values from the relevant dictionary print to file I get the following error: TypeError: string indices must be integers, not str

Well, when you iterate over a dictionary, you iterate over its keys, which is a string. And, to access a string you need a int value. That's why you get this error. In your loop, d is a string and you're trying to access it's value with another string instead a int.

Did you get it?

TypeError: string indices must be integers, not str, Lists are containers which can hold objects (such as dictionaries) or specific-type values (such as ints/strings). The below will throw an error on author because it is � TypeError: string indices must be integers, not str on Python Dictionary Direction list? This piece of code is from nested for loops, so I need to be able to

data is a dictionary if you use for instance:

mydict = {"item_1": 3, "item_2": 5}
for item in mydict:
    print(item)

it would print the dictionary keys:

item_1
item_2

That are strings, that's why if you try:

mydict = {"item_1": 3, "item_2": 5}
    for item in mydict:
        # item is a string here, so if you
        # Python complains about string indexes must be integers.
        item['overall-staked-amount']

it is exactly the same problem for comprehension.

You can get the value you want just by:

overall_staked_amount = data['overall-staked-amount']

You can iterate over keys and items by:

for key, value in data.items():
    # ...

dictionary trouble: string indices must be integers : learnpython, print(item[0]['temperature']) TypeError: string indices must be integers. What I am overlooking here? EDIT: I enclosed the dict in a list. Much easier for assignment� The above example uses the 0 index of the string to refer to the first character. Strings can't have string indices (like dictionaries can). So this won't work: >>> mystring = 'helloworld' >>> print mystring['stringindex'] TypeError: string indices must be integers

TypeError: string indices must be integers, not str on Python Dictionary, TypeError: string indices must be integers, not str on Python Dictionary of code is from nested for loops, so I need to be able to refer to the lists & dictionaries to� read JSON file error: " list indices must be integers or slices, not str" So, here is my code to read a JSON file. import json with open ( "example.json" ) as file : db = json . load ( file ) print ( db [ "supplier" ])

Python adding values to a dictionary: TypeError: string indices must , Python adding values to a dictionary: TypeError: string indices must be integers python dictionary list indices must be integers, not str string indices must be integers, not dict - nested values in dict Tag: python , dictionary I am trying to write � So, jsonStr is a string. Calling json.dumps on a string is perfectly legal. It doesn't matter whether that string was the JSON encoding of some object, or your last name; you can encode that string in JSON. And then you can decode that string, getting back the original string. So, this: j = json.loads(json.dumps(jsonStr))

python - TypeError: string indices must be integers, not str, Python - TypeError: string indices must be integers - Stack Overflow Transform dictionary object to a JSON-format, as an example of a serialisation aimed to this very situation and python - TypeError: string indices must be integers, not dict - nested. string - list indices must be integers not str python error - Stack Ov.. Dictionary with nested list TypeError: string indices must be integers TypeError: string indices must be integers, not str - OpenErp Pandas pd.merge “TypeError: string indices must be integers, not str”

Comments
  • I'm sorry I was meant to say attribute values instead of attributes. Will update my question
  • I also get an error TypeError: 'Response' object is not subscriptable I think this is due to casing on true and false values. Do you know what I can do to avoid this?
  • I'm not sure I follow. How do I assign an int value instead of d?
  • I actually meant attribute values instead of attributes. I have updated my question
  • Yes I know, I'm only demonstrating why the error you're receiving, you're iterating over the dictionary's keys which are strings, and then when you d['overall-staked-amount'] you are trying to apply a non-integer index to a string.
  • Use data.items() in order to iterate over the dictionary (key, value) pairs.