Interview Prep/Python for DevOps/Language essentials & functions

What are lambdas good for, and when should you avoid them?

BeginnerConceptLanguage essentials & functions
Answer

A lambda is a one-expression anonymous function. Good uses are short key functions:

newest = max(snapshots, key=lambda s: s["StartTime"])
by_cost = sorted(items, key=lambda x: (x["severity_rank"], -x["cost"]))

Avoid assigning lambdas to names (f = lambda x: ...; use def), multi-step logic, and lambdas created in loops that capture the loop variable. operator.itemgetter("StartTime") is a faster, clearer alternative for key lookups.

All Python for DevOps questions Roadmap: Python for automation

More on Language essentials & functions