54

I'm speaking of this module: http://docs.python.org/library/operator.html

From the article:

The operator module exports a set of functions implemented in C corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expression x+y. The function names are those used for special class methods; variants without leading and trailing __ are also provided for convenience.

I'm not sure I understand the benefit or purpose of this module.

1
  • 3
    This module seems kind of silly to me. All I can do with it is things I can easily do without it. It seems to be a clear violation of one of the principles in the Zen of Python: "There should be one-- and preferably only one --obvious way to do it." Commented Feb 15, 2015 at 0:57

6 Answers 6

49

Possibly the most popular usage is operator.itemgetter. Given a list lst of tuples, you can sort by the ith element by: lst.sort(key=operator.itemgetter(i))

Certainly, you could do the same thing without operator by defining your own key function, but the operator module makes it slightly neater.

As to the rest, python allows a functional style of programming, and so it can come up -- for instance, Greg's reduce example.

You might argue: "Why do I need operator.add when I can just do: add = lambda x, y: x+y?" The answers are:

  1. operator.add is (I think) slightly faster.
  2. It makes the code easier to understand for you, or another person later, looking at it. They don't need to look for the definition of add, because they know what the operator module does.
  3. operator.add is picklable, while lambda is not. This means that the function can be saved to disk or passed between processes.
4
  • "operator.add is faster": It seems it's not: $ python -m timeit "5 + 8" 100000000 loops, best of 3: 0.016 usec per loop $ python -m timeit -s "from operator import add" "add(5, 8)" 10000000 loops, best of 3: 0.0628 usec per loop Commented Dec 10, 2015 at 12:54
  • 14
    @MarcoSulla I'm not saying operator.add(5,8) is faster than 5+8. I'm saying I think it's faster than (lambda x,y: x+y)(5,8)
    – John Fouhy
    Commented Dec 10, 2015 at 21:31
  • 6
    Good catch: $ python -m timeit "(lambda x,y: x+y)(5,8)" 10000000 loops, best of 3: 0.127 usec per loop $ python -m timeit -s "from operator import add" "add(5,8)" 10000000 loops, best of 3: 0.0614 usec per loop Commented Dec 11, 2015 at 16:07
  • 1
    I use itemgetter and it's still useful in python 3.6, quick too
    – ragardner
    Commented Oct 16, 2017 at 20:53
33

One example is in the use of the reduce() function:

>>> import operator
>>> a = [2, 3, 4, 5]
>>> reduce(lambda x, y: x + y, a)
14
>>> reduce(operator.add, a)
14
8

for example get column in list whose member is tuple, sort sequence by column:

def item_ope():
    s = ['h', 'e', 'l', 'l', 'o']
    print operator.getitem(s, 1)
    # e
    print operator.itemgetter(1, 4)(s)
    # ('e', 'o')

    inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
    get_count = operator.itemgetter(1)
    print map(get_count, inventory)
    # [3, 2, 5, 1]

    print sorted(inventory, key=get_count)
    # [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]

see a more practical example, we want to sort a dict by key or value:

def dict_sort_by_value():
    dic_num = {'first': 11, 'second': 2, 'third': 33, 'Fourth': 4}

    # print all the keys
    print dic_num.keys()
    # ['second', 'Fourth', 'third', 'first']

    # sorted by value
    sorted_val = sorted(dic_num.items(), key=operator.itemgetter(1))
    # [('second', 2), ('Fourth', 4), ('first', 11), ('third', 33)]
    print sorted_val

    # sorted by key
    sorted_key = sorted(dic_num.items(), key=operator.itemgetter(0))
    print sorted_key
    # [('Fourth', 4), ('first', 11), ('second', 2), ('third', 33)]

another example when we want get the max value and it's index in list:

def get_max_val_idx():
    lst = [1, 7, 3, 5, 6]
    max_val = max(lst)
    print max_val
    # 7
    max_idx = lst.index(max_val)
    print max_idx
    # 1

    # simplify it by use operator
    index, value = max(enumerate(lst), key=operator.itemgetter(1))
    print index, value
    # 1 7

More demos like below:

import operator

def cmp_fun():
    a, b = 5, 3
    print operator.le(a, b)
    # False
    print operator.gt(a, b)
    # True


def lst_ope():
    lst = [1, 2, 3]
    print operator.indexOf(lst, 2)
    # 1
    lst1 = [1, 2, 3, 2]
    print operator.countOf(lst1, 2)
    # 2


def cal_ope():
    lst1 = [0, 1, 2, 3]
    lst2 = [10, 20, 30, 40]
    print map(operator.mul, lst1, lst2)
    # [0, 20, 60, 120]

    print sum(map(operator.mul, lst1, lst2))
    # 200

    a, b = 1, 3
    print operator.iadd(a, b)
    # 4

see more from python doc

2
  • +1 for the argmax example. I've often regretted the lack of built-in argmax and argmin functions in python.
    – saulspatz
    Commented Jan 21, 2018 at 14:14
  • Great post, Jayhello
    – Jon
    Commented Feb 24, 2019 at 18:53
5

The module is useful when you need to pass a function as an argument to something. There are then two options: use the operator module, or define a new function (using def or lambda). If you define a function on the fly, this can create a problem if you need to pickle this function, either to save it to disk or to pass it between processes. While itemgetter is picklable, dynamically defined functions (either with def or lambda) are not. In the following example, replacing itemgetter with a lambda expression will result in a PicklingError.

from operator import itemgetter

def sort_by_key(sequence, key):
    return sorted(sequence, key=key)

if __name__ == "__main__":
    from multiprocessing import Pool

    items = [([(1,2),(4,1)], itemgetter(1)),
             ([(5,3),(2,7)], itemgetter(0))]

    with Pool(5) as p:
        result = p.starmap(sort_by_key, items)
    print(result)
1
  • 1
    The keyphrase here is this -- "The module is useful when you need to pass a function as an argument to something". I can't think of any other reasons to use the module.
    – Jarmos
    Commented Aug 12, 2020 at 9:40
2

In general, the purpose of this module (as alluded to by some of the answers, above) is to provide you with canned functions for simple operations you would otherwise have to write yourself and pass to higher-order function such as sort() or reduce().

For example, without operators, to sum the numbers in a list, you would have to do something like this:

from functools import reduce

l = list(range(100))
f = lambda x, y: x + y
result = reduce(f, l)
print(result)

With the operator module, you could make use of its add() function like this:

from operator import add

result = reduce(add, l)
print(result)

Thus avoiding the need to create a lambda expression.

1
  • 1
    To sum up a list, sum should be used. sum(range(100)) Commented Oct 19, 2023 at 12:48
0

I cannot remember the exact use case but I had a requirement somewhere that needed to do some calculation dynamically and this could be using a different operator depending on where it came from.

A very simple example is this:

import operator

def add_or_subtract(x, y, op):
    return op(x, y)

x = 3
y = 10
o = operator.add #operator.sub

add_or_subtract(x, y, o)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.