• + 0 comments

    With Counter in Collection Module:

    from collections import Counter
    
    n= int(input())
    inp= [input() for _ in range(n)]
    counter_word = Counter(inp)
    
    key = len([key for key in counter_word.keys()])
    value = " " .join([str(value) for value in counter_word.values()])
    print(key)
    print(value)
    

    Without Counter in Collection module (Conventional Approach):

    n= int(input())
    inp= [input() for _ in range(n)]
    
    inp_unique= []
    count_all = []
    
    for i in inp:
        count= 1
        if i not in inp_unique:
            inp_unique.append(i)
            count_all.append(count)           
        else:
            inp_unique.index(i)
            count_all[inp_unique.index(i)] = count_all[inp_unique.index(i)] + 1
    
    print(len(inp_unique))
    print(" ".join((map(str, count_all))))