Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dictionaries Teacher Stats

How to traverse within items in a dictionary? I'm traversing over the last item but not over all items (key/value pair)

I've spent considerable amount of time trying to figure this out myself but came to the point where some help will be appreciated.


teachers = {'name':'Kenneth Love', 'course':['Python Basics', 'Python Collections'], 'name':'Jason Seifer', 'course':['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'name':'Jason Red', 'course':'OcaLoca'}

def most_classes(teachers):
    max_count = 0
    la_lista = []
    la_lista = teachers.values()

    for c in teachers:
        print (c, teachers[c])

    print("Values are now in this list: ", la_lista)
    for elements in la_lista:
        max_count = max_count + 1
    print("This is the number of elements in the list: ", max_count)

Final part of the code is properly giving me numbers of items/elements in the list that comprises values from the directory, that is fine.

The issue is that the initial for loop "for c in teachers" seem to be giving me information from the last key/value pair in the dictionary and not actually looping over it, seem like is only reading/handling 'name':'Jason Red', 'course':'OcaLoca' and not cycling over all elements in "teachers".


2 Answers

Thomas Kirk
Thomas Kirk
5,246 Points


The dictionary in your code is set up differently to that in the question. The example in the question is:

{'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}

You have put 'name' and 'course' in front of each of your keys and values:

{'name': 'Kenneth Love', 'course':['Python Basics', 'Python Collections'], 'name': 'Jason Seifer', 'course':['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']}

This is making your keys just duplicates of 'name', and 'course'. I don't believe Python supports duplicate keys, which may be why it is only returning the last instance of each. Perhaps someone else can confirm this?

Your keys should be, for example :

'Jason Seifer'

'Kenneth Love'

and values:

['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']

['Python Basics', 'Python Collections']

Hope that helps

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Correct, you cannot have duplicate keys.

Thank you so much for your response Thom, seem like Kenneth have already confirmed what you said and it is indeed a 'learning experience' (that was a string, right? haha) for me so I'll work with that input and will go ahead and award you with "best answer"....

BTW not sure why question box here in treehouse separated directory and definition of functions+variables before the black box where the for loop starts so here I uploaded an image for better visualization of it: http://mydevhost.net/vd/forNoLoop.png