How To Use Python Operator Module With Example

The Python Operator module allows it to support functional programming. Suppose we need a function to calculate factorial, the general approach is to use recursion. If you use functional programming, there are two ways, one is to use lambda, the other is to use the arithmetic function of the Python Operator module.

1. Use Lambda To Implement Factorial Calculation.

Below is the source code of using lambda to implement factorial calculation.

# Import reduce function from functools module.
from functools import reduce

# Define a function to implement factorial calculation.
def fact(n):
  Call the reduce function and pass a lambda to it as the first parameter.
  reduce(function, iterable[, initializer]): The first function parameter is a function which should has two parameters. The second parameter is a collection type object that can be 


  The reduce() function will first pass the first and second elements of the second collection parameter to the first function parameter to process, and then pass the result with the third    

  element of the second collection parameter to the function parameter to process again, and finally get a result.

  In this example, the function parameter in the reduce() function uses a lambda expression. The second input parameter, Iterable, is generated using the range() method
  return reduce(lambda a, b: a * b, range(1, n + 1))

2. Use Python Operator Module mul Function To Implement Factorial Calculation.

By contrast, the code is more concise by using the calculation function in the Python Operator module. Below is the source code of the Python Operator module mul function.

def mul(a, b):
  "Same as a * b."
  return a * b

Below is the source code that calculates the number factorial use python operator module’s mul function.

# Import the mul function from python operator module.
from operator import mul

# This function will calculate the number factorial use the above mul function.
def factWithMul(n):
  return reduce(mul, range(1, n + 1))

3. Operator Module Conditional Filter Functions.

3.1 Function itemgetter.

Suppose an f function is defined as f = itemgetter (2), then if f (r) is called, it will actually return r[2]. That means it will only return the part of the input parameter r whose index value is 2 (calculated from 1).

The application scenario of the itemgetter function is to sort the tuple list according to a field of the tuple. For example, we have a piece of city data in JSON format, and we need to sort the tuple data by the city’s abbreviations.

# Import the operator module itemgetter function.
from operator import itemgetter

# Define a list that contains city data.
metro_areas = [('Tokyo', 'JP', 36.933, (35.689722, 139.691667)), ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
        ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
        ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
        ('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),

# Sort the city data by the city abbreviation. Each element in the city data list is a tuple. The itemgetter function takes the second value of the tuple as the sorting key of the sorted function.
for city in sorted(metro_areas, key=itemgetter(1)):'city -> %s', city)

Below is the above code running results:

INFO - city -> ('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))
INFO - city -> ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889))
INFO - city -> ('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
INFO - city -> ('Mexico City', 'MX', 20.142, (19.433333, -99.133333))
INFO - city -> ('New York-Newark', 'US', 20.104, (40.808611, -74.020386))

If more than one input parameter is passed into the itemgetter function, it is a filter function that only filters out the required columns in the tuple object. For example, we only need the city abbreviation ( index is 1 )  and coordinates ( index is 3 ) in the city array then we can use the below soruce code.

cc_name = itemgetter(1, 3)
for city in metro_areas:'city -> %s', cc_name(city))

Below is the above code running results:

INFO - city -> ('JP', (35.689722, 139.691667))
INFO - city -> ('IN', (28.613889, 77.208889))
INFO - city -> ('MX', (19.433333, -99.133333))
INFO - city -> ('US', (40.808611, -74.020386))
INFO - city -> ('BR', (-23.547778, -46.635833))

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.