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 (Retired) Dictionaries Word Count

word_dict

def word_count(arg) word_dict = {} word_dict[arg] = count if arg in dict: count += 1

my code is starting to confuse me now!!

2 Answers

I won't show you the solution, but I can point you in the right direction.

Firstly, remember that you are asked to count the occurrences of each word in the input string (which you have called arg). To do this you must isolate each word in the input string. You can do this with the split() function. Check example below:

my_string = "This is a string"
list_from_my_string = my_string.split()

# my_string.split() returns ['This', 'is', 'a', 'string']

From there you must go through the list of words that you got from using the split() function and count each time you see it. For each word you must ask yourself: If I haven't seen this word before, then I must insert the word and set its count to 1. If I have seen it before, I must add 1 to the count.

Lastly, remember that you can add a new key-value pair to a dictionary by using the word as the key and the count as the value. If the key doesn't exist in the dictionary, Python will create it.

my_dictionary[new_key] = value

I hope this helps. Don't hesitate to ask again if you're still stuck.

oooohhh kaay. The last part was the sum of my confusion. I was trying to use update() which takes two parameters, and then I couldn't figure out how to get the key and value into the update.

HANKS

No problem, glad I could help you out.