Demo entry 6729666

sadsdasda

   

Submitted by anonymous on Apr 03, 2018 at 14:41
Language: Python 3. Code size: 340 Bytes.

# How to sort a Python dict by value
# (== get a representation sorted by value)

>>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}

>>> sorted(xs.items(), key=lambda x: x[1])
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]

# Or:

>>> import operator
>>> sorted(xs.items(), key=operator.itemgetter(1))
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).