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

csr13
csr13
33,250 Points

I think this works, what do you think?

Idk if this works. It sure outputs the dictionary with the word count on my interpreter, unless there is a comma here and there Idk.

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(to_split):
    beta = to_split.lower().split(' ')
    d = dict.fromkeys(beta, 0)
    for _ in beta:
        if _ in d.keys():
            d[_] += 1
    return d

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi carlaos91

That will work if you only want to split on spaces and not all whitespace. But what happens if what's being passed in used, for example, tabs and returns? These are considered whitespace, but not spaces, so in that instance, the split wouldn't work.
You just need to change what is being passed into the split() method to split on all whitespace and not just spaces.

Otherwise... Nice work!! :) :dizzy: