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.
More on Language essentials & functions
Why is Python the default scripting language in so many DevOps teams, and where would you not use it?BeginnerWhat is the difference between mutable and immutable types? Give examples.BeginnerWhat does assignment do in Python, and how do shallow and deep copies differ?BeginnerExplain the mutable default argument bug and how to fix it.IntermediateWhat is the difference between `==` and `is`?BeginnerWhat values are "falsy" in Python, and why does it matter in scripts?Beginner