Search code examples
pythondictionarytuplesmin

Return key corresponding to the tuple with smallest third value from dictionary


I am trying to return the key corresponding to the tuple with smallest third index value from a dictionary of tuples (a namedtuple vertex as key and a tuple with 3 elements as value).

For example, suppose I have the tuple:

vertex = namedtuple("Vertex", ["vertex_id", "vertex_x", "vertex_y"])
d = {vertex(vertex_id='B', vertex_x=11, vertex_y=0): (4, 5, 9), 
        vertex(vertex_id='C', vertex_x=6, vertex_y=0): (2, 0, 2), 
        vertex(vertex_id='A', vertex_x=4, vertex_y=0): (0, 2, 3)}

I need something to return me Vertex(vertex_id='C', vertex_x=6, vertex_y=0). I was trying something like min(d.values(), key = lambda t: t[2]) (but this returns the tuple (2, 0, 2) and I would have to trace it back to its key) or min(d, key = lambda t: t[2]) (this isn't really working).

Is there a better way to set min() to do this or do I have to trace back the key corresponding to the value that the first way gives me? It would be more efficient if I don't have to search for it when working with larger dictionaries.


Solution

  • Use min() with custom key= function. You can search the minimum over the tuples of d.items():

    print( min(d.items(), key=lambda k: k[1][2])[0] )
    

    Prints:

    Vertex(vertex_id='C', vertex_x=6, vertex_y=0)