Leo505 Leo505 - 26 days ago 6
Python Question

Accessing list of objects inside a dictionary

How would i print out dictionary values that are a list of objects. More specifically attributes from the objects

Output from a print of the dictionary

for keys,values in Dict.items():
print(keys)
print(values)

Five
[<VocabContent: Seven - 7>, <VocabContent: Ten - 10>, <VocabContent: Six - 6>, <VocabContent: Eight - 8>, <VocabContent: Five - 5>]
Eight
[<VocabContent: Ten - 10>, <VocabContent: Eight - 8>, <VocabContent: Six - 6>, <VocabContent: Five - 5>, <VocabContent: Seven - 7>]


These objects have spanish and audio attributes. I'm trying to print the objects spanish attribute with

for keys,values in Dict.items():
print values.spanish


But it does not work. My webpage just returns a server error 500

How can i access the object attributes from the dictionary

Thanks

Answer

You have to loop over the values of the list

for keys,values in Dict.items():
    for value in values:
        print(value.spanish)