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 (2016, retired 2019) Dictionaries Teacher Stats

horus93
horus93
4,333 Points

Brain melted from teacher task 4, stumped on 5

I'd attach the code but I keep coming up dry on this one, maybe it'll come to me later but I figured it as time to take a break and ask the community for input on the matter.

so the task for those not familiar is writing a function that prints out a list of lists, with dictionary keys and the total number of associated values (because this dictionary list of teachers all have multiple values in the form of class names, from what I can tell from researching people don't seem to like this kind of practice but here I am dealing with it now(and understandably so, if you understand how to deal with difficult problems, the easier methods will be a easier to adopt, and you'll be better of for it).

Anyway yea, so far the best i've been able to manage was listing the keys, and each had a 1 next to it (since the values of each one is that they have 1 list, so we have to dig further in and count these sublists within the dictionary, which is really the part busting my brain, ironically because I accidentally stumbled onto that during challenge #4, but threw it away since it wasn't relevant to the current problem (though, the results still weren't printed as lists in a list, so not quite there.).

So trying to think through the problem in my head, probably creating more problems than necessary. I'm thinking obviously we need a for in loop (maybe more than 1) to iterate through the dictionary, but also as they don't have index values I was having issues visualizing how to dynamically recombine the everything if I went through the trouble of separating out the keys an their associated values into separate lists of lists, then counted the internal list containing the target value parameters and printing each.

The challenge glues me to the computer, but I have to rip myself away again for a while so I don't eat up another entire day and a half making little or no headway.

2 Answers

Steven Parker
Steven Parker
231,275 Points

I agree you may be over-thinking it a bit. I found task 4 to be the hardest, and you've already gone past that one.

I'm not sure what you did that returned "1" next to each key, but if you use the "items" method on a dictionary as a loop iterable, it will allow the key and corresponding value can be retrieved at the same time. Then using the "len" function on the value will give you the number of items it contains.

And you are correct that dictionaries don't have index values, but the key serves as the way to access the value just as an index would do for a list.

Hopefully this will give you the ideas to get started, and if you have any further issues you can post your code for some specific help.

Happy coding! :christmas_tree: And for holiday-season fun and coding practice, give Advent of Code a try! :santa:

horus93
horus93
4,333 Points

Thanks that helped get me halfway there, but I run into an annoying little problem wherein print will return all of the keys, with a corresponding variable integer of the value, but if I use return instead it only returns the first key + number of values associated with it, but seems to just stop there, so it was strange to me that return acted differently, and after trying a few different things that failed like using a while loop (I thought, maybe i can have it go through the dictionary until it runs out of items, but then remembered that it's also not modifying the dictionary, so the while loop just runs indefinitely.). So right now I'm blanking on how to get the last part of this working (and am probably already overthinking it again).

My initial gut reaction thinks I need a blank list to store the values from the for loop

def stats(tt):
    for key, value in tt.items():
         return [key, len(value)]

annnnd then I thought "no, I'm so close, I can't throw in the towel just yet, this is freakin simple why isn't this clicking", so I went back to review some list stuff and figured it out, bringing me to here and finally getting over that last hump, feeling very satisfied lol.

def stats(tt):
    nlist = []
    for key, value in tt.items():
        slist = []
        slist.append(key), slist.append(len(value))
        nlist.append(slist)
    return nlist
Steven Parker
Steven Parker
231,275 Points

Congratulations on the eventual solution!

Happy coding! :christmas_tree: And for some holiday-season fun and extra practice, give Advent of Code a try! :santa: