Python - Itertools.starmap()
Last Updated :
12 Jul, 2025
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is starmap().
Note: For more information, refer to Python Itertools
starmap() function
When an iterable is contained within another iterable and certain function has to be applied on them, starmap() is used. The starmap() considers each element of the iterable within another iterable as a separate item. It is similar to map(). This function comes under the category terminating iterators.
Syntax :
starmap(function, iterable)
The function can be a built-in one or user-defined or even a lambda function. To understand the difference between map() and starmap() look at the code snippets below :
Python3
li =[(2, 5), (3, 2), (4, 3)]
new_li = list(map(pow, li))
print(new_li)
Output: TypeError Traceback (most recent call last)
in
1 li=[(2, 5), (3, 2), (4, 3)]
----> 2 new_li=list(map(pow, li))
3 print(new_li)
TypeError: pow expected at least 2 arguments, got 1
Here, the map considers each tuple within the list as a single argument and thus the error raises. The starmap() overcomes this issue. Look at the code snippet below:
Python3
from itertools import starmap
li =[(2, 5), (3, 2), (4, 3)]
new_li = list(starmap(pow, li))
print(new_li)
The internal working of the starmap() can be implemented as given below.
def startmap(function, iterable):
for it in iterables:
yield function(*it)
Here 'it' also denotes an iterable.
Let us look at another example that differentiates map() and starmap(). We can apply a function to each element in the iterable using map(). To say we need to add a constant number to each element in the lists, we can use map().
Python3
li =[2, 3, 4, 5, 6, 7]
# adds 2 to each element in list
ans = list(map(lambda x:x + 2, li))
print(ans)
Output: [4, 5, 6, 7, 8, 9]
What if we want to add different numbers to different elements of the list ?
Now, starmap() must be used.
Python3
from itertools import starmap
li =[(2, 3), (3, 1), (4, 6), (5, 3), (6, 5), (7, 2)]
ans = list(starmap(lambda x, y:x + y, li))
print(ans)
Output: [5, 4, 10, 8, 11, 9]
Practical example of using starmap():
Consider a list containing coordinates of various triangles. We are supposed to apply Pythagoras theorem and find output which coordinates form a right-angled triangle. It can be implemented as given below :
Python3
from itertools import starmap
co_ordinates =[(2, 3, 4),
(3, 4, 5),
(6, 8, 10),
(1, 5, 7),
(7, 4, 10)]
# Set true if coordinates form
# a right-triangle else false
right_triangles = list(starmap(lambda x, y, z:True
if ((x * x)+(y * y))==(z * z)
else False, co_ordinates))
print("tuples which form right angled triangle :",
right_triangles, end ="\n\n")
print("The right triangle coordinates are :",
end ="")
# to print the coordinates
for i in range (0, len(right_triangles)):
if right_triangles[i]== True:
print(co_ordinates[i], end =" ")
Output:
tuples which form right angled triangle : [False, True, True, False, False]
The right triangle coordinates are :(3, 4, 5) (6, 8, 10)
Similar Reads
Python Itertools Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and we wa
12 min read
Python - Itertools.count() Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it co
3 min read
Python - Itertools.cycle() Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators
3 min read
Python - itertools.repeat() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re
2 min read
Itertools.accumulate()-Python itertools.accumulate() is an iterator that takes two arguments, an iterable (target) and an optional function. The function is applied at each iteration to accumulate the result. By default, if no function is provided, it performs addition. If the input iterable is empty, the output will also be emp
3 min read
Python - Itertools.chain() The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is
4 min read
Python - Itertools.chain.from_iterable() Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Th
2 min read
Python - Itertools.compress() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co
2 min read
Python - Itertools.dropwhile() Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more inform
1 min read
Python - Itertools.filterfalse() In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() funct
2 min read