Sruli
Sruli

Reputation: 365

Why doesn't this method for looping through a dictionary work

I have a dictionary within a dictionary. I am trying to return the innermost dictionary ie the one with the keys name, teacher etc. To loop through I tried this.

courses = {
'feb2012': { 'cs101': {'name': 'Building a Search Engine',
                       'teacher': 'Dave',
                       'assistant': 'Peter C.'},
             'cs373': {'name': 'Programming a Robotic Car',
                       'teacher': 'Sebastian',
                       'assistant': 'Andy'}},
    'jan2044': { 'cs001': {'name': 'Building a Quantum Holodeck',
                       'teacher': 'Dorina'},
           'cs003': {'name': 'Programming a Robotic Robotics Teacher',
                       'teacher': 'Jasper'},
                 }
}

for e in courses:
     for y in e:
         return courses[e][y]

The console returns key error, what am I doing wrong?

Upvotes: 0

Views: 50

Answers (3)

RemcoGerlich
RemcoGerlich

Reputation: 31260

If you were to do:

for e in courses:
    print e

You'd find that it prints "feb2012", "jan2044" -- the values are strings.

So the for y in e: on the next line iterates through the characters of those strings.

You meant

for e in cources:
    for y in cources[e]:
        return courses[e][y]

However, because you return there, you'll only ever find one of the inner dictionaries. I wonder if that's what you need.

To get all of them, one of the ways could be to make this a generator, with yield instead of return:

def get_inner(courses):
    for e in courses:
        for y in courses[e]:  # Aside, these variable names are horrible
            yield courses[e][y]

And now you could loop through them with for innerdict in get_inner(courses): ....

But there are many ways...

Upvotes: 5

Megan S
Megan S

Reputation: 21

for k,v in courses.items():             #Iterates through Month/Year keys (feb2012, jan2014
    for k2,v2 in v.items():             #Iterates thought class identifer keys (cs101, cs373, cs001,cs003)
        for k3, v3 in v2.items():       #Iterates though class information (name, teacher, assistant)
            print('Semester: ' + k + '\tCourse: ' + k2 + '\tKey: ' + k3 + '\tValue: ' + v3) #Displays class information

Output:

Semester: feb2012   Course: cs101   Key: assistant  Value: Peter C.
Semester: feb2012   Course: cs101   Key: teacher    Value: Dave
Semester: feb2012   Course: cs101   Key: name   Value: Building a Search Engine
Semester: feb2012   Course: cs373   Key: assistant  Value: Andy
Semester: feb2012   Course: cs373   Key: teacher    Value: Sebastian
Semester: feb2012   Course: cs373   Key: name   Value: Programming a Robotic Car
Semester: jan2044   Course: cs001   Key: teacher    Value: Dorina
Semester: jan2044   Course: cs001   Key: name   Value: Building a Quantum Holodeck
Semester: jan2044   Course: cs003   Key: teacher    Value: Jasper
Semester: jan2044   Course: cs003   Key: name   Value: Programming a Robotic Robotics Teacher

Upvotes: 0

cppcoder
cppcoder

Reputation: 23115

for x in courses:                #Iterate through the outermost keys
    for y in courses[x]:         #Iterate through the second outer most keys
        return courses[x][y]:    #Return the innermost dictionary

Upvotes: 0

Related Questions