Introduction
In Python, you can use the min() function to find the minimum value of a list. However, what if you want to find the minimum value of a list based on a specific property or attribute of each element in the list? This is where the min_by() function comes in handy.
Find the Minimum Value of a List Based on a Function
Write a function called min_by(lst, fn) that takes a list lst and a function fn as arguments. The function should map each element in the list to a value using the provided function, and then return the minimum value.
def min_by(lst, fn):
return min(map(fn, lst))
min_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) ## 2
Summary
In this challenge, you learned how to find the minimum value of a list based on a specific property or attribute of each element in the list using the min_by() function. This function maps each element in the list to a value using a provided function, and then returns the minimum value.