python-3.xlistdictionaryminimum

python3 best method to yield minimum dict-pair in a list


indices_video = [{'list_index': 0, 'stream_index': 0}, {'list_index': 2, 'stream_index': 5}]

Hello, I have a couple of lists like the above.

Is there a quick way to yield the pair {list_index, stream_index} which contains the minimum value of 'stream_index' in the entire list ?

In the above example the result would be {'list_index': 0, 'stream_index': 0}

I could loop through the list, but thought someone may have a better way.

Thanks


Solution

  • Yes, you can do this by passing a function used to determine the order as the key parameter:

    min(my_list, key=lambda x: x['stream_index'])