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

Kunal Malik
Kunal Malik
767 Points

Answer not getting accepted to the word count problem. Need to make a dictionary with keys as words and values as freq

My answer to the problem word_count.py is not getting accepted. The problem states return a dictionary with each word as keys and freq as values, if passed a string to the function.

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(s):
    a = dict()

    for l in s.lower().split():
        if l in a.keys():
            a[l]=a[l] + 1
        else:
            a[l]=1
    return a

1 Answer

Kunal Malik
Kunal Malik
767 Points

I realized I do not need to explicitly specify .split(' ') instead just doing a .split() works.