Python Forum

Full Version: How to print counter without bracket in python and sort data.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
If you put sorted() around counter.items() they will come out sorted.