← All notes

Python operator - itemgetter, attrgetter, methodcaller and function forms of operators

Answer: itemgetter(), attrgetter() and methodcaller() each return a callable object that fetches an item, fetches an attribute, or calls a method on its operand, which suits key= arguments. The operator module exports efficient functions corresponding to the intrinsic operators of Python.

Examples

from operator import itemgetter, attrgetter, methodcaller, add, mul
from functools import reduce

rows = [("apple", 3), ("pear", 1), ("fig", 2)]
sorted(rows, key=itemgetter(1))          # by second item
itemgetter(0, 1)(rows[0])                # ('apple', 3)

class User:
    def __init__(self, name, age):
        self.name, self.age = name, age

users = [User("ana", 31), User("bo", 25)]
sorted(users, key=attrgetter("age", "name"))

words = ["  a ", " b"]
list(map(methodcaller("strip"), words))  # ['a', 'b']

reduce(mul, [1, 2, 3, 4])                # 24
list(map(add, [1, 2], [10, 20]))         # [11, 22]

Getter helpers

| Function | Returns a callable object that | |---|---| | itemgetter(2) | fetches item 2 with __getitem__(); several items return a tuple | | attrgetter('name') | fetches the attribute; names can contain dots like 'name.first' | | methodcaller('name', 'foo', bar=1) | calls obj.name('foo', bar=1) |

Operator functions

In-place functions

x += y is equivalent to x = operator.iadd(x, y). The in-place functions only call the in-place method; you still assign the result yourself. For immutable targets such as strings and numbers, the value is computed but not stored back.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, operator - Standard operators as functions; python/cpython (GitHub), Lib/operator.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, operator - Standard operators as functions; python/cpython (GitHub), Lib/operator.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:q015277k

iinventedlight. 2026. "Python operator - itemgetter, attrgetter, methodcaller and function forms of operators." iinventedlight Library, iil:note:q015277k, version of 2026-11-18. https://iinventedlight.com/library/notes/python-operator/. License: CC-BY-4.0.

@misc{iil_note_q015277k,
  author = {{iinventedlight}},
  title = {{Python operator - itemgetter, attrgetter, methodcaller and function forms of operators}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/python-operator/},
  note = {Permanent ID iil:note:q015277k. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-operator.md.