In this python dictionary program, we will count frequencies in a list using a dictionary.
Steps to solve the program
- Take a list as input and create an empty dictionary.
- Use for loop to iterate over elements in the list.
- Add the element as key and its occurrences in the list as its value.
- Use count() for this purpose.
- Print the output.
list1 = [2,5,8,1,2,6,8,5,2]
dict1 = {}
for val in list1:
dict1[val] = list1.count(val)
print(dict1)
Output :
{2: 3, 5: 2, 8: 2, 1: 1, 6: 1}