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

I can see my items have appended to their lists, but I'm only getting a length of 1

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

jason = []
kenneth = []


def most_classes(teachers_dict):
    for key in teachers_dict:
        if key == "Jason Seifer":
            jason.append(teachers_dict[key])
        else:
            kenneth.append(teachers_dict[key])

    print(jason)
    print(len(jason))
    print(kenneth)
    print(len(kenneth))

# is returning this in the console
# I need the length of the lists for comparison
# I can see three items in the first list
# but I only get a len of 1???
# [['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']]
# 1
# [['Python Basics', 'Python Collections']]
# 1

    if len(jason) > len(kenneth):
        return "Jason Seifer"
    else:
        return "Kenneth Love"


result = most_classes(teachers_dict)
print(result)

I just noticed that when I append my items to their respective lists, multiple items go in as one item. So I guess I need to fix that. Google here I come

2 Answers

Hi John, Yea it seems like your own answer nailed it on that one. You're creating a list of lists. Basically a list containing the courses which will count as one element as its inside another list.

Just something quick and dirty to see if this might help:

def most_classes(teachers_dict):
    for key in teachers_dict:
        if key == "Jason Seifer":
            for course in teachers_dict[key]:  # Now iterate each class inside the teacher key.
                jason.append(course)
        else:
            for course in teachers_dict[key]:
                kenneth.append(course)

Daniel, I will try that right now... Well, that fixed it, thank you so much. Now I'll spend some time meditating on that so I can really grasp whats happening there.

Sure no probs John,

You just have to consider what it is you are returning in your loop:

for key in teachers_dict:
        if key == "Jason Seifer":
            jason.append(teachers_dict[key])  # <== this returns a list of courses.

So for teachers_dict['Jason Seifer'] will return: ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']

You are then appending that entire list into your Jason-list. When you append a list to another list, the appended list will only count as a single element.

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

# Get the first element in jason:
jason[0]  # Returns ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']

# Since we only have 1 element (a list), doing a len will return 1:
len(jason)  # Returns 1

# What the above additional loop does is appends each item under the teacher key in teachers_dict as an individual item in the jason list.

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

# Now you get an accurate count:
len(jason)  Returns # 3

Thanks, that's a really good breakdown. I love systematic explanations.