operator

Updated March 13, 2026 · Modules
stdlib operators functional-programming

The operator module exports functions that correspond to Python’s built-in operators. These functions are useful when you need to pass an operator as an argument to a function like map(), sorted(), itertools.reduce(), or when building callable objects dynamically.

Arithmetic Operators

These functions perform basic arithmetic operations:

import operator

# Addition
result = operator.add(3, 5)
print(result)  # 8

# Subtraction
result = operator.sub(10, 4)
print(result)  # 6

# Multiplication
result = operator.mul(3, 7)
print(result)  # 21

# Division (true division)
result = operator.truediv(15, 4)
print(result)  # 3.75

# Floor division
result = operator.floordiv(15, 4)
print(result)  # 3

# Modulo
result = operator.mod(17, 5)
print(result)  # 2

# Power
result = operator.pow(2, 8)
print(result)  # 256

# Negation
result = operator.neg(5)
print(result)  # -5

# Positive
result = operator.pos(-3)
print(result)  # 3

Comparison Operators

These functions return boolean results:

import operator

# Equality
print(operator.eq(5, 5))   # True
print(operator.eq(5, 3))   # False

# Not equal
print(operator.ne(5, 3))   # True

# Less than
print(operator.lt(3, 5))    # True
print(operator.lt(5, 3))    # False

# Less than or equal
print(operator.le(5, 5))    # True

# Greater than
print(operator.gt(5, 3))    # True

# Greater than or equal
print(operator.ge(5, 5))    # True

# Truth value testing
print(operator.truth(0))    # False
print(operator.truth(1))   # True
print(operator.truth([]))   # False
print(operator.truth([1]))  # True

Logical Operators

import operator

# Boolean and
print(operator.and_(True, True))   # True
print(operator.and_(True, False))  # False

# Boolean or
print(operator.or_(False, True))   # True
print(operator.or_(False, False))   # False

# Boolean not
print(operator.not_(True))   # False
print(operator.not_(False))  # True

# Logical containment (is)
x = [1, 2, 3]
print(operator.contains(x, 2))  # True
print(operator.contains(x, 5))   # False

Bitwise Operators

import operator

# Bitwise and
print(bin(operator.and_(0b1100, 0b1010)))  # 0b1000

# Bitwise or
print(bin(operator.or_(0b1100, 0b1010)))   # 0b1110

# Bitwise xor
print(bin(operator.xor(0b1100, 0b1010)))  # 0b110

# Bitwise not
print(bin(operator.invert(0b1010)))  # -0b1011

# Left shift
print(operator.lshift(1, 4))  # 16

# Right shift
print(operator.rshift(16, 4))  # 1

Item and Attribute Access

import operator

# Get item (like subscript)
data = {"name": "Alice", "age": 30}
print(operator.getitem(data, "name"))  # Alice

# Set item
data = {"name": "Alice"}
operator.setitem(data, "age", 31)
print(data)  # {'name': 'Alice', 'age': 31}

# Delete item
data = {"name": "Alice", "age": 30}
operator.delitem(data, "age")
print(data)  # {'name': 'Alice'}

# Get attribute
class Person:
    def __init__(self):
        self.name = "Bob"

p = Person()
print(operator.attrgetter("name")(p))  # Bob

# Get nested attribute
class Nested:
    def __init__(self):
        self.data = {"value": 42}

n = Nested()
print(operator.attrgetter("data.value")(n))  # 42

Callable Factories

These create callable objects for dynamic access:

import operator

# itemgetter - extract items from sequences/mappings
get_first = operator.itemgetter(0)
print(get_first([1, 2, 3]))  # 1

get_name = operator.itemgetter("name")
print(get_name({"name": "Alice", "age": 30}))  # Alice

# attrgetter - extract attributes
get_name = operator.attrgetter("name")
class Person:
    name = "Bob"
print(get_name(Person))  # Bob

# methodcaller - call methods on arguments
upper = operator.methodcaller("upper")
print(upper("hello"))  # HELLO

split = operator.methodcaller("split", " ")
print(split("a b c"))  # ['a', 'b', 'c']

Common Patterns

Using with map()

import operator

list1 = [1, 2, 3]
list2 = [10, 20, 30]

# Element-wise addition
result = list(map(operator.add, list1, list2))
print(result)  # [11, 22, 33]

Using with sorted()

import operator

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __repr__(self):
        return f"Person({self.name}, {self.age})"

people = [Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35)]

# Sort by age
by_age = sorted(people, key=operator.attrgetter("age"))
print(by_age)  # [Person(Bob, 25), Person(Alice, 30), Person(Charlie, 35)]

# Sort by name
by_name = sorted(people, key=operator.attrgetter("name"))
print(by_name)  # [Person(Alice, 30), Person(Bob, 25), Person(Charlie, 35)]

Using with functools.reduce()

import operator
from functools import reduce

# Product of all elements
numbers = [2, 3, 4, 5]
product = reduce(operator.mul, numbers)
print(product)  # 120

# Concatenate strings
words = ["Hello", " ", "World", "!"]
result = reduce(operator.add, words)
print(result)  # Hello World!

Using with max() and min()

import operator

data = [
    {"name": "Alice", "score": 85},
    {"name": "Bob", "score": 92},
    {"name": "Charlie", "score": 78}
]

highest = max(data, key=operator.itemgetter("score"))
print(highest)  # {'name': 'Bob', 'score': 92}

lowest = min(data, key=operator.itemgetter("score"))
print(lowest)  # {'name': 'Charlie', 'score': 78}

Using with itertools

import operator
import itertools

# Accumulate with operator
numbers = [1, 2, 3, 4, 5]
result = list(itertools.accumulate(numbers, operator.add))
print(result)  # [1, 3, 6, 10, 15]

See Also