Is there a pythonic way to sort Counter by value? If so, it is faster than this:

```>>> from collections import Counter
>>> x = Counter({'a':1, 'b':2, 'c':3})
>>> sorted(x)
['a', 'b', 'c']
>>> sorted(x.items())
[('a', 1), ('b', 2), ('c', 3)]
>>> [(l,k) for k,l in sorted([(j,i) for i,j in x.items()])]
[('a', 1), ('b', 2), ('c', 3)]
>>> [(l,k) for k,l in sorted([(j,i) for i,j in x.items()], reverse=True)]
[('c', 3), ('b', 2), ('a', 1)```

Nov 13, 2018 in Python 1,369 views

## 1 answer to this question.

>>> x = Counter({'a':1, 'b':2, 'c':3})
>>> sorted(x.items(), key=lambda k: -k[1]) # Ascending
[('a', 1), ('b', 2), ('c', 3)]

• 58,090 points

## How to sort Counter by value using python?

Use the Counter.most_common() method, it'll sort the items for you: >>> ...READ MORE

## How to sort dictionary by value python

This would work: d = sorted(data, key = ...READ MORE

## How do I sort a dictionary by value?

If you construct a dictionary with the ...READ MORE

## Sort list by frequency in python

You can try the following for a ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE