Python Forum
How to print counter without bracket in python and sort data. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to print counter without bracket in python and sort data. (/thread-20059.html)



How to print counter without bracket in python and sort data. - phob0s - Jul-25-2019

How to print counter without bracket and sort result in python ?

I need to have this final result :

2 : 2
3 : 3
4 : 4
5 : 3
7 : 2
But I'v got this :

Counter({4: 4, 3: 3, 5: 3, 2: 2, 7: 2})
Here is my code :

counter=collections.Counter(lst)
    print(counter)
    counter.keys()
    for key, value in counter.items():
        #print(key,":", value)
        ctx = (key, value)
        print(ctx)
    tab.append(ctx)
    tab.sort()
    #print(tab)
    #print(*tab, sep = "\n")
Or this result but not sorted with this print options :

print(key,":", value)
3 : 3
4 : 4
2 : 2
5 : 3
7 : 2
Thanks for any help. Big Grin


RE: How to print counter without bracket in python and sort data. - ichabod801 - Jul-25-2019

If you put sorted() around counter.items() they will come out sorted.