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

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 68,441 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 68,441 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 68,441 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!