Suppose I have a function designed to find the largest Y
value in a list of dictionaries.
s1 = [
{'x':10, 'y':8.04},
{'x':8, 'y':6.95},
{'x':13, 'y':7.58},
{'x':9, 'y':8.81},
{'x':11, 'y':8.33},
{'x':14, 'y':9.96},
{'x':6, 'y':7.24},
{'x':4, 'y':4.26},
{'x':12, 'y':10.84},
{'x':7, 'y':4.82},
{'x':5, 'y':5.68},
]
def range_y(list_of_dicts):
y = lambda dict: dict['y']
return y(min(list_of_dicts, key=y)), y(max(list_of_dicts, key=y))
range_y(s1)
This works and gives the intended result.
What I don't understand is the y
before the (min(list_of_dicts, key=y)
. I know I can find the min
and max
with min(list_of_dicts, key=lambda d: d['y'])['y']
where the y
parameter goes at the end (obviously swapping min
for max
).
Can someone explain to me what is happening in y(min(list_of_dicts, key=y))
with the y
and the parenthetical?