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

Sean Flanagan
Sean Flanagan
33,235 Points

Can't figure this out

Hi.

I can only create the function along with its required arguments. I can't figure out the rest.

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):
    word_list = 0

3 Answers

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

We know that it wants us to return a dictionary of word counts. So start off by creating a variable with an empty dictionary.

We also know that it's going to give us a string that is a sentence with some words in it, so we can use the split method on strings to give us back a list of words.

We know that we need to convert the string and words to lowercase

For each word in the string, we want to count how many times it's in the string, and put that count into the dictionary

Let me know if you have more questions.

Sean Flanagan
Sean Flanagan
33,235 Points

Thanks for your help Brendan Whiting.

I know what the split() and lower() methods do but I'm not sure what to use them on.

Here's what I've got so far:

# 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):
    word_list = 0
    dictionary = {}
    for word in string:
        word_list += 1
Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

You can call those methods on the string parameter: string.lower().split(). You'll end up with a list of lowercase words.

Also, what we want to end up with is so that they dictionary has key value pairs where the key is a word and the value is the number of times that word occurs. The example they gave us is that word_count("I do not like it Sam I Am") would produce {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}. So as you loop through the words, you want to be making changes to the dictionary object, rather than the word_list.

One way to do this is check to see if that word already is a key in the dictionary. If it is, increment the value by one. If it's not, add that key to the dictionary with the value 1.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Brendan.

I've cracked this with your help and the help of psculthorpe.

Thank you both!

I've got this, which works fine in my Workspace, but will not be accepted as the answer. Confused :(

def word_count(string):
    words = string.lower().split(" ")
    dictionary = {}
    for word in words:
        if word not in dictionary:
            dictionary[word] = 1
        else:
            dictionary[word] += 1
    return dictionary
Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

split(" ") is only going to split on space characters. split() with no arguments will default to splitting on any whitespace character, including tabs and new line characters.

Of course! Thanks Brendan Whiting :)