- Counter() is a python function defined in collections module that are used to convert the list into the dictionary according to the occurence of the item in the list.
-Number of key in dictionary is equal to the number of distinct elements in list.
Syntax-
collections.Counter(seq)
-It return the dictionary of python.
Example-
list is =['a','a','b','a','c','c']
then collections.Counter(list) will return {'a':3,'b':1,'c':2}
program-
#import the collections module
import collections
#input of the list
print("Enter item in list")
list=input().split()
print("nList is :",list)
dict=collections.Counter(list)
print("Dictionary is :",dict)
Enter item in list
a b a b a c c c c b d a
List is : ['a', 'b', 'a', 'b', 'a', 'c', 'c', 'c', 'c', 'b', 'd', 'a']
Dictionary is : Counter({'a': 4, 'c': 4, 'b': 3, 'd': 1})