extracting tuples based on values in python
Question: I have a list of tuples with following format ("key","value"), I need to
extract keys with highest & second highest values & store them, how do I
achieve this in python ?


Answer: Use
[`heaq.nlargest`](http://docs.python.org/2/library/heapq.html#heapq.nlargest):

    
    
    import heapq
    heapq.nlargest(2, list_of_t, key=lambda x:x[1])
    

**Demo:**

    
    
    >>> import heapq
    >>> list_of_t = [('a', 100), ('b', 5), ('c', 50)]
    >>> heapq.nlargest(2, list_of_t, key=lambda x:x[1])
    [('a', 100), ('c', 50)]
    

