Python - Minimum in each record value list

Python - Minimum in each record value list

The objective here is to find the minimum value in each "record value list" from a given data structure. Let's assume that you have a dictionary where each key represents some record and the associated value is a list of numbers.

Objective:

For each key in the dictionary, find the minimum number from its list.

Example:

Given dictionary:

data = { "record1": [5, 7, 3, 8], "record2": [2, 8, 9], "record3": [10, 3, 5, 7] } 

The desired output is:

{ "record1": 3, "record2": 2, "record3": 3 } 

Implementation:

You can achieve this using dictionary comprehension:

def find_min_in_records(data): return {key: min(value) for key, value in data.items()} data = { "record1": [5, 7, 3, 8], "record2": [2, 8, 9], "record3": [10, 3, 5, 7] } print(find_min_in_records(data)) 

This function will return a new dictionary where each record has its minimum value from the associated list.


More Tags

kivy lasagne immutable.js git-remote filestream linker instagram root relationship marshalling

More Programming Guides

Other Guides

More Programming Examples