Sorting a Dictionary by Value then Alphabetically by Key Python3
Question: Let's say I have a dictionary like:

    
    
    h=[('a',5), ('f', 3), ('b',3), ('c',3), ('d',1), ('e',4) ]
    

I want it sorted like:

    
    
    [('a',5), ('e',4), ('b',3), ('c',3), ('f',3), ('d',1)]
    

I can solve this with Python 2 with something like this:

    
    
    sortedList= sorted(h.iteritems(),key=lambda(k,v):(-v,k))
    

I can get really close in Python 3 with something like this:

    
    
    import operator
    sortedList =sorted(h.items(), key=operator.itemgetter(1,0) , reverse=True)
    

but it comes out like this

    
    
    [('a',5), ('e',4), ('f',3), ('c',3), ('b',3),  ('d',1)]
    

How can I reverse the tiebreaker operation?


Answer: You can use this call to the sorted function in python 3:

    
    
    sortedList = sorted(h, key=lambda k: (-k[1], k[0]))
    

This will give the same result as the python 2 sorting:

    
    
    [('a',5), ('e',4), ('b',3), ('c',3), ('f',3), ('d',1)]
    

