Created
September 6, 2021 20:55
-
-
Save joan0fsnark/f80a008244b3e821b40188e4f3e4d0ad to your computer and use it in GitHub Desktop.
Function that takes a list of elements named 'words' as a parameter and returns a dictionary containing the frequency of each element in 'words'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def frequency_dictionary(words): | |
dict = {} | |
for word in words: | |
if word not in dict: | |
dict[word] = 0 | |
dict[word] += 1 | |
return dict | |
# Uncomment these function calls to test your function: | |
print(frequency_dictionary(["apple", "apple", "cat", 1])) | |
# should print {"apple":2, "cat":1, 1:1} | |
print(frequency_dictionary([0,0,0,0,0])) | |
# should print {0:5} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment