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

reza sajadian
reza sajadian
718 Points

Can anybody help me with my quiz?

This is my quiz:

Create a function named word_count() that takes a string. Return a dictionary with each word in the string as the key and the number of times it appears as the value.

and this is my answer:

my_dict = {}

def word_count(string):

  stn_string = string.lower()
  mylist = stn_string.split()
  replist = mylist[:]              # replist(as of replicated list) Keeps the original listed string intact
  for word in replist:
      if word in my_dict:
        my_dict[word] += 1
        my_dict[word] = 1
return my_dict

2 Answers

Ken Alger
Ken Alger
Treehouse Teacher


Take a look at the great answer Tree Casiano provided on this question in this post.

Hope it helps and happy coding,

reza sajadian
reza sajadian
718 Points

Thanks Ken. It was completely helpful. thumbs up.

Ken Alger
Ken Alger
Treehouse Teacher

Pleased it helped and welcome to Treehouse!