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

Word Count dictionary challenge. Can't seem to figure it out!

I watched each video several times and really thought I had a good understanding of this until I hit the challenge! I tried following the layout from the examples and that doesn't seem to be working. I know it's missing the lowercase part, I wasn't sure where to add that step. When I tried looking into it every solution I saw looked absolutely nothing like this and I just became even more confused. Really want to know for sure that I understand this, if someone could try and help me understand that'd be great!

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("How are you"):
    return {'How': 1, 'are': 1, 'you': 1}

3 Answers

Hi Solzic,

It is important to note that when you are writing this function, your parameters should not be actual strings, but rather some variable name that you want to use to reference your parameter. The reason for this is you want to be able to take in any string as input, and use that string in your function.

Secondly, as Alex Koumparos describes, you can't be returning a dictionary literal. So what do you do?

def word_count(s):
    a = s.lower().split()
    word_dict = {}
    for word in a:
        word_dict[word] = word_dict.get(word, 0) + 1
    return word_dict

This is my answer that I just coded, and it works. The logic behind it is as follows. You want to have your function take in any arbitrary string, which as you can see I called s. Now, for the actual functionality.

First, I create a variable called a which is just an array of all the words of the string s, all lower case. If you are confused about this line, you can google "python split function" or "python lower function" to see what's going on there.

Then I initialize an empty dictionary, which I will be filling with the words from a in my loop.

In the loop, there are a couple things going on. The "word_dict.get(word,0)" part means that I am trying to access the key "word" in my word_dict dictionary and return the associated value from it. If it is not there, it will create it and initialize its associated value as zero. I then add 1 to this zero value. I put that resulting number into word_dict[word] (the left of the equals sign). The same principal applies if the key "word" is already in the dictionary. It will simply take the value that is associated with it, add one, and put that number into word_dict[word] (left of the equals sign, again). This process results in generating a dictionary with each unique word as a key and its associated count as the value.

I hope this makes sense, If you have any more questions don't hesitate to ask.

Chris

Thanks, Chris.

Your code and explanation make sense to me.

These tasks are often very confusing for me. Should be some hints on the task page. I have looked for answers and explanation for most of the tasks. my (human) memory is very poor. Like 640k poor

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Solzic,

In your function you are returning a dictionary literal, i.e., just typed in values. But the point of your function is to be able to take any string and return the number of times each word in that string appears.

At a high level, here's what you're going to need to do:
Step 0. Create an empty dictionary to hold your output
Step 1. break the string apart into its constituent parts (breaking on all whitespace) into some iterable data structure;
Step 2. Iterate (loop) through each word in your iterable data structure
Step 3. On each iteration, check if the word is already in your dictionary
Step 4a) if the word is not in your dictionary, add it as a key to the dictionary and make the value 1 (because you've seen the word 1 time)
Step 4b) else (the word is already in your dictionary) increment that key's value
Step 5. return the dictionary.

Cheers

Alex

Ragy Abuamra
Ragy Abuamra
4,044 Points

I was very confused here as well.. Till I saw your code Chris :)... That was very clear explanation and I really thank you. Cheers