How to sort a python dictionary by value

If you want to search a dictionary by value in python, here is a handy one line using also lambda:

mydict = {'ver1':'2002','ver2':'2000','ver3':'2020'}
dict_v_srt=sorted(mydict.items(),key=lambda sort_ver: sort_ver[1])
print dict_v_srt

If you run this script output will be ;

[('ver2', '2000'), ('ver1', '2002'), ('ver3', '2020')]

About: rtoodtoo

Worked for more than 10 years as a Network/Support Engineer and also interested in Python, Linux, Security and SD-WAN // JNCIE-SEC #223 / RHCE / PCNSE

You have a feedback?

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading