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

Genco has worked for more than 10 years as a Network/Support Engineer. He is also interested in Python, Linux, Security and SD-WAN, currently lives in the Netherlands and works as a Network Support Engineer at Tesla Inc. // JNCIE-SEC #223 / RHCE / PCNSE

You have a feedback?

This site uses Akismet to reduce spam. Learn how your comment data is processed.