Skip to content

Instantly share code, notes, and snippets.

@joan0fsnark
Created September 6, 2021 20:55
Show Gist options
  • Save joan0fsnark/f80a008244b3e821b40188e4f3e4d0ad to your computer and use it in GitHub Desktop.
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'.
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