Q:

Python nested dict top N values with highest confidence

I have a dict, which looks like this:
{ 
"name1" : { "title":"someTitle", "confidence" : 0.765 },
"name2" : { "title":"someTitle2", "confidence" : 0.9343 },
"name3" : { "title":"someTitle3", "confidence" : 0.0031 },
"name4" : { "title":"someTitle4", "confidence" : 0.23 },
"name5" : { "title":"someTitle5", "confidence" : 0.8768 }
}

Out of such a dict I need to create another similar dict, containing only values with highest confidence value.  In this case that should be: name2, name5, name1. 
{ 
"name1" : { "title":"someTitle", "confidence" : 0.765 },
"name2" : { "title":"someTitle2", "confidence" : 0.9343 },
"name5" : { "title":"someTitle5", "confidence" : 0.8768 }
}

Dict structure and contents should be preserved. 
Thanks! 

A:

dict(sorted(d1.items(), key=lambda item: item[1]['confidence'])[-3:])

