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

anhedonicblonde
anhedonicblonde
3,133 Points

Create a function named word_count...

I'm doing a review of my Python studies and finding the parts where I didn't get things the first time around. I'm having trouble seeing what is wrong with my code for the word_count challenge - can someone tell me where I have gone wrong? Thanks so much!

word_count.py
# E.g. word_count("I am that I am") gets back a dictionary like:
# {'i': 2, 'am': 2, 'that': 1}
# Lowercase the string to make it easier.
# Using .split() on the sentence will give you a list of words.
# In a for loop of that list, you'll have a word that you can
# check for inclusion in the dict (with "if word in dict"-style syntax).
# Or add it to the dict with something like word_dict[word] = 1.
def word_count(string):
  my_string = string.lower()
  word_list = my_string.split()
  word_dict = {}
  count = 0

  for word in word_list:
    if word in word_dict:
      count +=1
    else:
      word_dict[word] = 1
  return count

3 Answers

Anish Walawalkar
Anish Walawalkar
8,534 Points

Hey, you're doing great, you just made a few small mistakes:

  1. the question does not ask you to lowercase the string, so you do not need to do it
  2. you should check using word in word_dict.keys() thought just checking word in word_dict is not wrong
  3. The question asks you to return a dictionary mapping words to their count not an overall count

here is the code:

def word_count(string):
  word_list = string.split()
  word_dict = {}
  for word in word_list:
    if word in word_dict.keys():
      word_dict[word] +=1
    else:
      word_dict[word] = 1
  return word_dict

Hope this helps

anhedonicblonde
anhedonicblonde
3,133 Points

Oh, cool! Thank you so much!! I really appreciate it- I understand now, thank you :)

Andrey Chernykh
PLUS
Andrey Chernykh
Courses Plus Student 2,552 Points

Easier way would be count()

The method count() returns count of how many times obj occurs in list.

def word_count(string):
  my_string = string.lower()
  word_list = my_string.split()
  word_dict = {}

  for word in word_list:
    word_dict[word]=word_list.count(word)
  return word_dict
Andrey Chernykh
PLUS
Andrey Chernykh
Courses Plus Student 2,552 Points

Just a small addition , there is no need of using .keys() in this case , try to go easy and with less code