operator.itemgetter()
operator.itemgetter() is a Python function in the Operator Module category. Returns a callable that fetches the specified item(s) from its operand. The syntax is operator.itemgetter(*items). Parameters: *items — indices or keys. Returns: Callable getter. A typical example: from operator import itemgetter
students = [("Alice", 85), ("Bob", 92)]
sorted(students, key=itemgetter(1), reverse=True)
# [("Bob", 92), ("Alice", 85)]. A close sibling is operator.add(), which returns a + b (addition). Useful with reduce and map. A close sibling is operator.sub(), which returns a - b (subtraction). A close sibling is operator.mul(), which returns a * b (multiplication). A close sibling is operator.attrgetter(), which returns a callable that fetches the specified attribute(s) from its operand. More about this category: Operator functions — add, mul, itemgetter, attrgetter, methodcaller. Related Operator Module entries: operator.add(), operator.sub(), operator.mul(), operator.attrgetter(), operator.methodcaller(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.