Python Itertools.starmap() Function with Examples

Itertools Module:

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.

Itertools.starmap() Function:

When an iterable is contained within another iterable and a function must be applied to both of them, starmap() is used. Each element of an iterable within another iterable is treated as a separate item by starmap(). It is comparable to a map (). This function belongs to the category of terminating iterators.

A built-in function, a user-defined function, or even a lambda function can be used.

Syntax:

starmap(function, iterable)

Parameters

function: This is Required. Procedures to be executed on iterable data.

iterable: This is Required. Elements that will be passed to the function.

Examples:

Example1:

Input:

Given List = [(6, 2), (5, 5), (2, 9), (7, 1), (0, 3)]
function: lambda a, b: a + b

Output:

[8, 10, 11, 8, 3]

Example2:

Input:

Given List = [(6, 2), (5, 5), (2, 9), (7, 1), (0, 3)]
function: lambda a, b: a * b

Output:

[12, 25, 18, 7, 0]

Itertools.starmap() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Using Lambda Function

Approach:

  • Import starmap() function from itertools using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass lambda() function and given list as the arguments to the starmap() function where lambda() function adds the two numbers and store it in another variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import starmap() function from itertools using the import keyword.
from itertools import starmap
# Give the list as static input and store it in a variable.
gvn_lst = [(6, 2), (5, 5), (2, 9), (7, 1), (0, 3)]
# Pass lambda() function and given list as the arguments to the starmap() function
# where lambda() function adds the two numbers and store it in another variable.
rslt = starmap(lambda a, b: a + b, gvn_lst)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print(rslt_lst)

Output:

[8, 10, 11, 8, 3]
2)Function Using def

Approach:

  • Import starmap() function from itertools using the import keyword.
  • Create a function say Multiplication which accepts three numbers as arguments and returns the multiplication of three numbers.
  • Inside the function, Return multiplication of given three arguments.
  • Give the list as static input and store it in a variable.
  • Pass the above function(Multiplication) and given list as the arguments to the starmap() function where Multiplication() function multiplies the three numbers and store it in another variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import starmap() function from itertools using the import keyword.
from itertools import starmap
# Create a function say Multiplication which accepts three numbers as arguments and
# returns the multiplication of three numbers


def Multiplication(p, q, r):
    # Return multiplication of given three arguments
    return p*q*r


# Give the list as static input and store it in a variable.
gvn_lst = [(2, 5, 4), (1, 10, 2), (3, 5, 2)]
# Pass the above function(Multiplication) and given list as the arguments to the
# starmap() function where Multiplication() function multiplies the three numbers
# and store it in another variable.
rslt = starmap(Multiplication, gvn_lst)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print(rslt_lst)

Output:

[40, 20, 30]