Sort dictionary by Python value

Asked

Viewed 14,521 times

5

I have a dictionary with the following format

dic={759147': 54, '186398060': 8, '199846203': 42, '191725321': 10, '158947719': 4}

would like to know if there is how to sort it by value and print on the screen. So that the output is.

'158947719': 4
'186398060': 8    
'191725321': 10
'199846203': 42
'759147': 54 

2 answers

6


2

A python dictionary has no order, in most cases it makes no sense to maintain the order of the dictionary items.

But there is the Ordereddict (also in python 2).

Alternatively you can sort a list with the keys and access the values in the order of that list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.