Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Word Count

Tyler Nordmann
Tyler Nordmann
1,083 Points

I need help with the word count in python collections. I run the script on my own and it works, but submission says no.

So I created a file in Workspaces to test my code and it works perfectly! Problem is that when I try to submit it it doesn't work. Anyone know why???

wordcount.py
def word_count(phrase):
    space_index = []
    key_list = []
    phrase = phrase.lower()
    count = 1

    for letter in phrase:
        upto = phrase[0:count]
        if letter == " ":
            space_index.append(len(upto)-1)
        count += 1

    count = 0
    key_list = [phrase[0:space_index[0]]]
    for num in space_index:
        try:
            nums = space_index[count:count+2]
            key_list.append(phrase[nums[0]+1:nums[1]])
            count += 1
        except IndexError:
            continue

    key_list.append(phrase[space_index[len(space_index)-1]+1:len(phrase)])

    counter = []
    count= 0
    for num in key_list:
        counter.append(count)

    count = 0
    for item in key_list:
        for thing in key_list:
            if thing == item:
                counter[count] += 1
        count += 1

    my_dict = dict(zip(key_list, counter))
    return my_dict

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,987 Points

You solution is certainly complex. While it may work on the sample input, it raises error when run on trivial inputs: "I" and "".

Hint: try using the .split() method to divide a string on whitespace. It returns a list of the words. You can loop over this new wordlist to count words.

Post back if you need more help. Good luck!!!

Tyler Nordmann
Tyler Nordmann
1,083 Points

Chris,

Thank you for the .split() advice! It greatly helped simplify my code, but it still is not working during submission. Any thoughts why? I have tried it with many different strings and it seems to work for all of them.

def word_count(phrase):

    phrase = phrase.lower()

    key_list = phrase.split()

    counter = []
    count= 0
    for num in key_list:
        counter.append(count)

    count = 0
    for item in key_list:
        for thing in key_list:
            if thing == item:
                counter[count] += 1
        count += 1

    my_dict = dict(zip(key_list, counter))

    return my_dict
Chris Freeman
Chris Freeman
Treehouse Moderator 67,987 Points

I couldn't figure out what the error was so I tried running your code as is and it passed the challenge.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,987 Points

It's very unusual to have two nested loops that operate on the same iterable. In this case, looping over key_list affectively is counting all the occurrences for every item. This is effectively an order n-squared performance that will quickly degrade as the word list grows.

A solution to consider is how to walk down the key_list one time while updating an existing dictionary each time a word is seen.

Tyler Nordmann
Tyler Nordmann
1,083 Points

Chris,

I ran it a few more times in a row and after the third try it accepted it. Thank you for the help. And yes, I am quite new to Python so the nested for loop is most likely extremely degrading. I will be sure to try alternate routes in the future. Thanks again for the help!