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 Word Count

Alan Kuo
Alan Kuo
7,697 Points

I'm out of my mind, please help!

Please see my code.

This is the answer for word count challenge, I copied the answer from others and completed my challenge but couldn't understand it even after I copied others' work...

my questions will be the second line said: dict = {} which is a blank dictionary, how come in 5th line it said: if word .lower() in dict:, and every letter that's split into a list is now IN the dictionary.

Totally stuck on this one.

wordcount.py
# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.
def word_count(string):
    dict = {}
    list = string.split()
    for word in list:
        if word.lower() in dict:
            dict[word.lower()] += 1
        else:
            dict.update({word.lower(): 1})
    return dict

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Alan Kuo! First, I'd like to applaud you for reaching out and trying to understand the answer. So many are far too quick to accept the answer and try to move on without understanding. So kudos to you! :thumbsup:

I feel like part of your misunderstanding stems from the .split() function. This takes a string and splits it on all whitespace. This includes spaces, tabs and newline characters. It's not splitting that string into letters, rather it's splitting it into words. Once we have that we go through each word in that list which this person has aptly named word. If the word already exists in the dictionary, we add 1 to its count. So if we've already seen it 3 times and now we're seeing it again, the count will be 4. The else clause here says that if the word is not found (it's the first time we've seen it), then add it to the dictionary and set its count to 1. After all, we did just see it once :smiley:

So I'm going to re-post the code with comments and see if that is enlightening in any way:

def word_count(string):
    # create an empty dictionary
    dict = {}
    # turn the string into a list and split it into words
    list = string.split()
    #l ook at each word in the list
    for word in list:
        # if the lower case version of the word exists in the dictionary increment the count by 1
        if word.lower() in dict:
            dict[word.lower()] += 1
        # if the lower case version of the word cannot be found, add it to the dictionary and set its count to 1
        else:
            dict.update({word.lower(): 1})
     # send back the completed dictionary
    return dict

Hope this helps! :sparkles:

Alan Kuo
Alan Kuo
7,697 Points

I finally understood this, it uses the if else function to loop all the words back into the dictionary and loop it over and over again.

Thank you Jennifer.