Dictionary Python call keys

0

I can not call my key name value. I get error: string indices must be integers, not str . I need if or if I use the for but I do not understand why I get that error

diccionario = {
    "inventorysources":(
        {
            'name': 'DK $ 2 |',
            'device': 'desktop',
            'cost':2,
            'floor':2.5,
            'publisher':"5a01e576becbf9000255f29c"
        }
    )
}

for inv in diccionario["inventorysources"]:
    name = inv['name']
    print name
    
asked by Martin Bouhier 09.11.2017 в 14:08
source

1 answer

1

When you execute the for loop, you are iterating through the dictionary elements inventorysources , with name being one of those elements. Or what is the same, inv will have at some point the value of name .

I suspect that your mistake is that you wanted inventorysources to be a tuple of dictionaries. Realize that right now, as it is, it is not a (mono) tuple , the parentheses are too many.

If it were a monotuple you would not have problems:

diccionario = {
    "inventorysources":(
        {
            'name': 'DK $ 2 |',
            'device': 'desktop',
            'cost':2,
            'floor':2.5,
            'publisher':"5a01e576becbf9000255f29c"
        }
    ,)
}

Look at the comma in the penultimate line that converts the item into monotuple.

    
answered by 09.11.2017 в 14:34