Python Python Collections (2016, retired 2019) Dictionaries Word Count

Jacob Walter
Jacob Walter
4,209 Points

copy = {}

What is this line of code doing exactly? On the word count code challenge and saw an answer using this as part of the code. Not quite sure what that means step by step here is the full code:

def word_count(word): word = word.lower() words = word.split()
copy = {}

for item in words:
    if item in copy.keys():
        copy[item] = copy[item] + 1
    else:
        copy[item] = 1
return copy

1 Answer

Josh Keenan
Josh Keenan
17,380 Points

Copy is an empty dictionary you are using to store the words in as keys, and then the frequency of the word is the value