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

I'm almost there I feel. Would appreciate a hint

Here's the code challenge So here's my code so far:

dictionary = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections',"Alpha","Beta"], "Zryab": ["Sigma", "Theta", "Gamma"]}

def stats(dictionary):
    stats_list = []
    consolidated = []
    for keys, values in dictionary.items():
        values = len(values)
        keys = keys
        stats_list.append(keys)
        stats_list.append(values)    
    return stats_list
stats(dictionary)

This in turn provides me with the output:

['Kenneth Love', 4, 'Zryab', 3, 'Andrew Chalkley', 2]

Unfortunately, it's not in a list of lists format. I've tried doing another loop such as the below:

def stats(dictionary):
    stats_list = []
    consolidated = []
    for keys, values in dictionary.items():
        values = len(values)
        keys = keys
        stats_list.append(keys)
        stats_list.append(values)    
    for whatever in stats_list:
        for coolio in whatever:
            consolidated.append(coolio)
    print(consolidated)
stats(dictionary)

This give me a TypeError. Any hints would be appreciated!

BRAD STYES
BRAD STYES
13,421 Points

The 'whatever's are scalar or singular items, not iterable items. What is the output you are looking for?

nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

are you trying to get each appended element to be a list within a list?

Sorry about that - here is the original code challenge

1 Answer

Jacob Richter
Jacob Richter
11,210 Points

You are so close:

def stats(dictionary):
    stats_list = []
    consolidated = []
    for keys, values in dictionary.items():
        values = len(values)
        keys = keys
        stats_list.append([values, keys]) #this is the adjustment you were looking for  
    return stats_list
stats(dictionary)

a cleaner version of your code would be:

def stats(dictionary):
    stats_list = []
    for keys, values in dictionary.items():
        stats_list.append([len(values), keys]) #this is the adjustment you were looking for  
    return stats_list
stats(dictionary)
nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

Mohammed, you should check out pythontutor.com to help with step by step visualization!!! Thanks for the cleaner version Jason, very helpful!!

Thanks for your help!