On this page
operator.itemgetter
Prev: operator.isub | Up: operator | Next: operator.itruediv
Rel: operator.abs, operator.add, operator.and_, operator.attrgetter
Ref: docs.python.org — operator.itemgetter
Tags: #public #python3 #stdlib #operator
Import
import operator
from operator import itemgetter
Signature & Overview
operator.itemgetter(item, /, *items)
Return a callable object that fetches the given item(s) from its operand.
Usage
import operator
rows = [("b", 2), ("a", 1), ("c", 3)]
assert sorted(rows, key=operator.itemgetter(1)) == [("a", 1), ("b", 2), ("c", 3)]
Gotchas
- Faster than
lambda x: x[1]: Passing multiple keys (itemgetter("a", "b")) returns a tuple.
REPL Inspect
from operator import itemgetter
help(itemgetter)
Linked from
Related notes
- tuts/python3/stdlib_module/operator/isub Linked here; links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/itruediv Linked here; links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/abs Linked here; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/add Linked here; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/and_ Linked here; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/attrgetter Linked here; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/call shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/concat shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/contains shared tags: #operator, #stdlib