• + 6 comments

    Why my code doesn't work?

    from collections import Counter
    string = sorted(Counter(input()).most_common(3), key= lambda x: (-x[1],x[0]))
    print("\n".join(x[0]+" "+str(x[1]) for x in string))
    

    but this code is working

    from collections import Counter
    string = sorted(Counter(input()).items(), key= lambda x: (-x[1],x[0]))[:3]
    print("\n".join(x[0]+" "+str(x[1]) for x in string))
    

    Why if I used most_common, I get wrong answer?