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

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Unsure of why I'm not getting the right count of words?

Hi - not sure about how to progress in this part - can you add items to dict usingg a counter e.g. dict[word]+=1?

Help appreciated!

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):
  dict={}
  string=string.lower()
  words=string.split()
  for word in words:
    dict[word]=0
    if word in words:
      dict[word]+=1
  return dict

6 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Dhruv, you're on the right track, I made some correction to you code, with extensive comment to help you understand.

def word_count(string):
  my_dict = {}     # don't use dict as variable name, becuase dict is a built-in function in Python
  word_list = string.lower().split()
  for i in word_list:
    if i in my_dict:    # if i key is already in the my_dict
      my_dict[i] += 1     # add 1 to the my_dict[i] lookup
    else:                # very important, else, the i in not in my_dict yet
      my_dict[i] = 1  # add i to the my_dict
  return my_dict
def word_count(s):
  L1 = s.lower().split()
  freq_dict = {}

  for k in L1:
      if k not in freq_dict:
          freq_dict[k] = 1

      else:
          freq_dict[k] += 1   

  return(freq_dict)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

return isn't a function so it doesn't need the parentheses (Python will ignore them, though).

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

because you have this line in your for loop

dict[word]=0

which always set the lookup value to 0, whether or not they are in the dictionary already.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

To answer your other question, yes, you can increment a value of a key with dict[key] += 1.

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Thanks William - makes total sense. I took that part out but still something is wrong with my code.

Any pointers? Sorry I am asking so many questions, it's the way I'm learning best:

def word_count(string):
  dict={}
  word_list=(string.lower()).split()
  for i in word_list:
    if i in word_list:
      dict[i]+=1
  return dict
Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Very helpful. 2 things learnt here - you are right, I need to add the keys to the dict via my_dict[i]=1, otherwisse nothing happens, and also I learn now not to use dict={} as an empty variable name. Thanks a lot!!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Using dict or list or whatever as a variable isn't the end of the world. But, if you ever need to go back to the class for something, you'll have issues. It's a practice that's better to not fall into.