• + 0 comments

    Before Python 3.7, the order is not guaranteed in dictionaries:

    from collections import Counter, OrderedDict
    
    class OrderedCounter(Counter, OrderedDict): 
        pass
    
    words = OrderedCounter(input() for _ in range(int(input())))
    print(len(words))
    print(*words.values())
    

    Since Python 3.7, the order is guaranteed to be kept in dictionaries which makes the code even simpler:

    from collections import Counter
    
    words = Counter(input() for _ in range(int(input())))
    print(len(words))
    print(*words.values())