sorting a counter in python by keys
dictionary, python, python-2.7
Solution
Just use sorted:
>>> from collections import Counter
>>> counter = Counter({'A': 10, 'C': 5, 'H': 7})
>>> counter.most_common()
[('A', 10), ('H', 7), ('C', 5)]
>>> sorted(counter.items())
[('A', 10), ('C', 5), ('H', 7)]
Problem
I have a counter that looks a bit like this: ``` Counter: {('A': 10), ('C':5), ('H':4)} ``` I want to sort on keys specifically in an alphabetical order, NOT by `counter.most_common()` is there any way to achieve this?