Python Glossary Русский

all Python 3operatorfunction

operator.lt

comparison

Return a < b — the functional form of the "less than" operator.

Syntax

operator.lt(a, b)

With functools.partial the argument order flips the meaning: partial(operator.lt, 0) is x → 0 < x, i.e. "greater than zero", not "less". Comparing unorderable types raises TypeError: lt(1, 'a') yields neither True nor False. You do not need lt for sorting — sorted compares by itself, and order is set with key and reverse.

Examples

import operator
print(operator.lt(1, 2))   # → True
print(operator.lt(2, 2))   # → False
print(operator.lt((1, 2), (1, 3)))   # → True
nums = [4, 1, 7]
print([n for n in nums if operator.lt(n, 5)])   # → [4, 1]
print(operator.lt(None, 1))   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy