Interview Prep/Python for DevOps/Language essentials & functions

How do `sorted()` and `list.sort()` differ, and how do you sort by multiple keys?

BeginnerConceptLanguage essentials & functions
Answer

sorted(iterable) returns a new list; list.sort() sorts in place and returns None (a common bug is x = x.sort()). Both are stable and accept key= and reverse=.

findings.sort(key=lambda f: (SEVERITY_RANK[f.severity], f.region, f.resource_id))

Tuple keys sort by the first element, then the next. Stability means you can also sort in multiple passes, least significant key first.

All Python for DevOps questions Roadmap: Python for automation

More on Language essentials & functions