operator.itemgetter
Functions from Python's operator module that build key functions: itemgetter fetches items by index or key and attrgetter fetches attributes.
In more detail
sorted(rows, key=itemgetter('dept', 'salary')) sorts records by department and then salary. They are shorter and slightly faster than equivalent lambda functions.
Related terms
- key function — A function given as key= to sorted, sort, min or max, which turns each item into the value to compare by.
- sorted() and list.sort() — Python's built-in sorts: sorted() returns a new sorted list from any iterable, and list.sort() sorts a list in place.
Taught in
- Sorting In Real Systems: Stability, Keys And External Sort — Algorithms & Data Structures for Working Engineers