Frequency of List Elements using Dictionary

Given a list of elements find the frequency of each distinct element in the list using python dictionary.

n = int(input())
li = list(map(int,input().split()))[:n]
d = {}
for ele in li:
  if ele not in d:
    d[ele] = li.count(ele)
for k,v in d.items():
  print(k,"-",v)
7
8 3 4 7 9 7 3
8 - 1
3 - 2
4 - 1
7 - 2
9 - 1

No comments:

Post a Comment

Total Pageviews