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

cheyenne hemmati
cheyenne hemmati
1,195 Points

Word_count question

I have gone thru this many times comparing correct answers but can someone explain to me what: dictionary[word] = string.lower().split().count(word)
is doing please. The = part is throwing me off.

def word_count(string): dictionary = {} for word in string.lower().split(): dictionary[word] = string.lower().split().count(word) return dictionary

wordcount.py
def word_count(string):
    dictionary = {}
    for word in string.lower().split():
        dictionary[word] = string.lower().split().count(word)
    return dictionary
cheyenne hemmati
cheyenne hemmati
1,195 Points

so its adding the word to the dictionary, but how does the key and the value get identified.

1 Answer

Steven Parker
Steven Parker
230,995 Points

The key is the term in the brackets, in this case the contents of "word".

The value is what is being assigned, so it is whatever the expression on the right side of the equal sign evaluates to.