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

I can't find the good solution to this challenge. My function get the right output but not in the same order.

I try many solutions but my dictonary still output in a different order.

Thanks for help Seb

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(phrase):
    dict = {}
    phrase = phrase.lower()
    tab = phrase.split(" ")
    for item in tab:
        dict.update({ item : tab.count(item)})
    return dict

3 Answers

Alexander Schott
PLUS
Alexander Schott
Courses Plus Student 937 Points
wordcount.py
def word_count(text):
    word_dict = {}
    word_list = text.lower().split(" ")
    for word in word_list:
        if word in word_dict:
            word_dict[word] += 1
        else:
            word_dict[word] = 1
    return word_dict

that works for me but doesn't get accepted. why? Thank you for your convenient solution!

btw. how did you add the filename to your markdown code example?

Hi Alexender

I had the same return. You just have to modify your split method from split(" ") to slipt().

In split method, the default separator is any white space.

Good luck Seb

I just find the solution myself, it was my split method.