Leo505 Leo505 - 1 year ago 83
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():

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


Answer Source

You have to loop over the values of the list

for keys,values in Dict.items():
    for value in values:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download