Question about lambda function as a key

Screen Link:
https://app.dataquest.io/m/355/list-comprehensions-and-lambda-functions/9/using-lambda-functions-to-analyze-json-data

Hello, I don’t understand why the lambda function is used as a key in hn_sorted_points
Normally I have a feeling why a certain solution is used, but here I don’t feel like I get it, can someone please explain to me?

hn_sorted_points = sorted(hn_clean, key=lambda d: d['points'], reverse=True)
top_5_titles = [d['title'] for d in hn_sorted_points[:5]]

@LuukvanVliet the key is argument which expects a function to determine the sorting order.

https://docs.python.org/3/library/functions.html#sorted

1 Like