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 trialDan Brubaker
15,084 PointsDict out of order? Solving WordCount challenge
My dict is not in the same order as the example. Can someone point me in the right direction for this? I've been stuck on it for a while...
# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.
def word_count(string):
keys = string.lower().split(" ")
counts = []
for word in keys:
count = [keys.count(word)]
counts = counts + count
dictionary = dict(zip(keys, counts))
return dictionary
4 Answers
Steven Parker
231,275 PointsDictionaries are orderless collections, by definition. When ordering is important, Python has another object type called "OrderedDict", but you don't need those for this challenge. The mechanism that checks your result will not be concerned about order.
But you're probably closer than you think, and the error message is giving you a hint: "Bummer: Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!"
To split on "all whitespace" the argument to "split" should be left empty or set to "None". Check the documentation on str.split() for further details.
Dan Brubaker
15,084 PointsMy code returns:
{'i': 2, 'do': 1, 'not': 1, 'like': 1, 'it': 1, 'sam': 1, 'am': 1}
I get a check error of Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!
Not sure what else to do... 🤷🏻♂️
Dan Brubaker
15,084 PointsAh, just saw your post edit. Checking it out... ^
Dan Brubaker
15,084 PointsYup! The .split() got it. Thank you!