Make a Frequency Dictionary From the Element of a List
·145 words·1 min
Hello, my new challenge is to write a python program that tally’s each letter for example there are 3 a’s, 2 b’s and 5 c’s in the dictionary it shall look like this {“a”: 3, “b”: 2, “c”: 5}
To do this:
- make a list it can be numbers if you want
- make an empty dictionary
- do for-loop
- use if statement and if the key not in dictionary add to the dictionary with the default value 1
- else/ if in there add 1 to the old key
- print the dictionary
#lelement = ["a", "a", "b", "c", "a", "b"]
lelement = [1, 2, 3, 4, 2, 1, 2]
dict = {}
print("New challenge: Make a Frequency Dictionary from the Elements of a List")
for t in lelement:
#print(t)
if t not in dict:
dict[t] = 1
else:
dict[t] = dict[t] + 1
print(dict)
Thank You. :3