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 (2016, retired 2019) Dictionaries Word Count

Trying to understand the solution for this challange

Hi!, so i have a few questions regarding the solution, i was unable to solve it by my self so i looked over a few solutions and i understood the idea of the solution but is till have questions: this is the code:

def word_count(string):
    fixed_string = string.lower().split()
    d = {}
    for key in fixed_string:
        count = 0
        for value in fixed_string:
            if value == key:
                count +=1
                print(value,key)
        d[key] = count
    return d

print(word_count("I do not like it Sam I Am"))

questions: I tried to see what actually the value in each time so i printed this is the results: https://gyazo.com/576445af798faf35ff7b978f025f216f I understand that in the first for loop we just loop over the list and the second for loop is meant to loop again on the list to see if the values are equal and if the value in the first for loop is equal to the value in the second for loop we increase count. but i dont really know how is it working. 1.#why i need to declare the count here and not outside of the for loop?, i tried to declare it outside of the loop and the counting messed up.

Asian Hospital Application Developer Dave StSomeWhere Steven Parker

8 Answers

Steven Parker
Steven Parker
230,274 Points

The reason to set the count to 0 inside the loop is because you want a separate count for each key. If you kept adding to to count for every key, then they would all be too large.

Steven Parker just to be sure i understood this, lets say we in the first iteration of fixed_string i have key which is "steven" then for this key i set his count to 0?

and why we nested the for loops?

Steven Parker
Steven Parker
230,274 Points

Yes, you first set the count to 0, but then the inner loop will total up the count of how many times that key is found. Then, when the outer loop moves on to the next key, it starts a new count from 0.

Nested loops is not the only strategy you can use for this task, but here the outer one is selecting each word and the inner one is counting how many times it occurs.

how the coutn is not rested lets say when when we get to the last "I" in the sentence if the count of each key is rested to 0 evrey iteration? im sorry i know its a stupid question

Steven Parker
Steven Parker
230,274 Points

The count does reset to 0 for every key with the code as shown above. But if you move the assignment outside the loop, it is only set to 0 one time before the loop starts, and then it would keep building up from then on.

lol i got confused again. each time the count does reset for each key, i didnt moved the count outside of the loop and it passed teh code challnages

Steven Parker
Steven Parker
230,274 Points

That's right, the code shown above should pass the challenge as it is. Your original question #1 was why it would not work if you moved the initialization of "count" out of the loop (I didn't see a #2).

you didnt understand my question, as the code now, each time count is set to 0 , what i want to know is how in the sentence: print(word_count("I do not like it Sam I Am")) this is basically : [I,DO, NOT, LIKE, IT, SAM, I , AM]

lets take the I key for example, it appear 2 times in the string, so if we do the iteration for the first I , then the output is now i =1, when we get to the seonc I, the I count is rest to 0 is it not?, that's what hard for me to understand how it builts up to 2 if its considered as a new key

Steven Parker
Steven Parker
230,274 Points

There are two "I"s in the phrase. So each time the key is "I", the count is first set to 0 in the outer loop, and then the inner loop runs and builds it up to 2.

It doesn't matter how many times any "key" appears. Every time, the inner loop will give the same count.

the counts inside the inner loop and in the outer loop are 2 different counts?

Steven Parker
Steven Parker
230,274 Points

Same "count". The outer loop sets it to 0 and the inner loop builds it up. Then the outer loop saves it in the new "d" dictionary before moving on to the next "key".

Thanks i finally understand it!

Andrew Bickham
Andrew Bickham
1,461 Points

I was wondering if i could possibly ask a question concerning this code?

Andrew Bickham
Andrew Bickham
1,461 Points

the only thing I didn't quite understand about this code was why the key and value had to be the same ex: if key == value:

Andrew Bickham u check if the key is equal to the value because the outer loop is selecting each word and the innerloop is counting how many times it occurs

Andrew Bickham
Andrew Bickham
1,461 Points

oooooooooooohhh, okay thank you bot .net