Itertool is a standard library in Python 3. The itertools module provides various foundational components for iterators, drawing inspiration from elements found in APL, Haskell, and SML. In layman's terms, the collection of iterators can collectively form an 'iterator algebra,' enabling the execution of intricate tasks.
The functionalities provided by itertools serve to generate more sophisticated iterators. The built-in zip function in Python can take an arbitrary number of arguments as input in the form of iterables. It processes tuples and yields their respective elements.
Python Itertools Example using zip Function
Let's consider an example to demonstrate the utilization of itertools in Python with the zip function.
a = [1,2,3]
b= ['a', 'b', 'c']
c = zip(a,b)
print(c)
Output:
[(1, 'a'), (2, 'b'), (3, 'c')]
Explanation:
In the code presented above, we have provided two lists, [1,2,3] and ['a', 'b', 'c'], as arguments to the zip function. These lists yield one element sequentially. In Python, an object that contains the .iter or .getitem method is referred to as an iterable.
The iter function in Python is utilized to invoke an iterable and produce an iterator object from that iterable.
Python Itertool Example
Let’s consider an example to demonstrate the functionality of the Python itertools module.
a = iter('Hello')
print(a)
Output:
<str_iterator object at 0x01505FA0>
Explanation:
The zip function in Python invokes iter on all of its inputs and subsequently uses next to merge the outcomes into a tuple.
Note: If you are using the zip function and map function, that means you are already using itertools. You don't need to import it distinctly.
Types of Iterators
There are various types of iterators in itertools module. The list is given below:
- Infinite Iterators
- Combinatoric Iterators
- Terminating Iterators
Infinite Iterators
In Python, any object that can be used in a for loop is called an iterator. Lists, tuples, sets, dictionaries, and strings are examples of iterators, but an iterator can also be infinite, and this type of iterator is called an infinite iterator.
| Iterator | Argument | Results |
|---|---|---|
| count(start,step) | start, [step] | start, start+step, step+2*step |
| cycle() | P | p0,p1,….plast |
| repeat() | elem [,n] | elem, elem, elem,….endlessly or upto n times |
1. count(start, stop)
It generates output from the initial value up to infinity. The step parameter is not mandatory; if a value is specified for the step, it will determine how many steps are to be omitted.
Python count(start, stop) Function Example:
To demonstrate the Python count(start, stop) function, let’s consider an example.
import itertools
for i in itertools.count(10,5):
if i == 50:
break
else:
print(i,end=" ")
Output:
10 15 20 25 30 35 40 45
2. cycle(iterable)
This iterator outputs all values sequentially from the provided argument. It displays the values in a repetitive cycle.
Python cycle(iterable) Example:
Consider an example to demonstrate the cycle function in Python, which is used for iterables.
import itertools
temp = 0
for i in itertools.cycle("123"):
if temp > 7:
break
else:
print(i,end=' ')
temp = temp+1
Output:
1 2 3 1 2 3 1 2 3 1 2
Example of Python Cycle(iterable) Utilizing the next Function:
To demonstrate the Python cycle(iterable) function, we can utilize the next function as an example.
import itertools
val = ['T', 'Point', 'Tech']
iter = itertools.cycle(val)
for i in range(6):
# Using the next function
print(next(iter), end = " ")
Output:
C# Tutorial C# Tutorial
3. repeat(val,num)
True to its name, it continuously outputs the provided value indefinitely. The argument num is not mandatory.
Python repeat(val, num) Example:
Consider an example to demonstrate the Python repeat(val, num) function.
import itertools
print("Printing the number repeadtly:")
print(list(itertools.repeat(40,15)))
Output:
[40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40]
Combinatoric Iterators
Recursive generators streamline intricate combinatorial structures. Examples of these combinatorial constructs include permutations, combinations, and Cartesian products.
In Python, there exist four categories of combinatorial iterators:
1. Product
This function is utilized to compute the Cartesian product of a given iterable. Within this function, the optional keyword argument repeat is employed to determine the product of an iterable with itself. The repeat keyword indicates how many times the iterable is to be repeated. The output is returned as sorted tuples.
Python Product Example:
Consider an example to demonstrate the product iterator in Python.
from itertools import product
print("We are computing cartesian product using repeat Keyword Argument:")
print(list(product([1, 2], repeat=2)))
print()
print("We are computing the Cartesian product of the containers:")
print(list(product(['T', 'Point', 'Tech'], '5')))
print()
print("We are computing the product of the containers:")
print(list(product('CD', [4, 5])))
Output:
We are computing the Cartesian product using the repeat Keyword Argument:
[(1, 1), (1, 2), (2, 1), (2, 2)]
We are computing the Cartesian product of the containers:
[('T', '5'), ('Point', '5'), ('Tech', '5')]
We are computing the product of the containers:
[('C', 4), ('C', 5), ('D', 4), ('D', 5)]
2. permutations
This function is utilized to produce every potential permutation of an iterable. The distinctiveness of each element relies on its position rather than its values. It takes two parameters: iterable and groupsize. If groupsize is not provided or is set to none, it defaults to the length of the iterable.
Python permutations Iterator Example:
Let us consider an example to demonstrate the permutations iterator available in Python.
from itertools import permutations
print("Computing all permutation of the following list")
print(list(permutations([3,"Python"],2)))
print()
print("Permutations of following string")
print(list(permutations('AB')))
print()
print("Permutation of the given container is:")
print(list(permutations(range(4),2)))
Output:
Computing all permutations of the following list
[(3, 'Python'), ('Python', 3)]
Permutations of the following string
[('A', 'B'), ('B', 'A')]
Permutation of the given container is:
[(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)]
3. Combinations
It is utilized to generate all potential combinations (without repetition) of the provided container, which is given as an argument in the designated group size, arranged in a sorted manner.
Python Combinations Iterator Example:
To exemplify the combinations iterator in Python, let us consider a specific case.
from itertools import combinations
print("Combination of list in sorted order(without replacement)",list(combinations(['B',3],2)))
print()
print("Combination of string in sorted order",list(combinations("ZX",2)))
print()
print("Combination of list in sorted order",list(combinations(range(20),1)))
Output:
Combination of list in sorted order(without replacement) [('B', 3)]
Combination of string in sorted order [('Z', 'X')]
Combination of list in sorted order [(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)]
4. Combination_with_replacement
It takes two parameters: the initial parameter is a tuple of length r, and the second parameter specifies the number of repetitions. It provides a subsequence of length n derived from the elements of the iterable and continues this method. Distinct elements may occur multiple times in combinationwithreplacement.
Illustration of the combinationwithreplacement Iterator in Python:
To demonstrate the functionality of the combinationwithreplacement iterator in Python, let us consider an example.
from itertools import combinations_with_replacement
print("Combination of string in sorted order(with replacement) is:")
print(list(combinations_with_replacement("XY", 3)))
print()
print("Combination of list in sorted order(with replacement) is:")
print(list(combinations_with_replacement([4, 2], 3)))
print()
print("Combination of container in sorted order(with replacement) is:")
print(list(combinations_with_replacement(range(3), 2)))
Output:
Combination of string in sorted order(with replacement) is:
[('X', 'X', 'X'), ('X', 'X', 'Y'), ('X', 'Y', 'Y'), ('Y', 'Y', 'Y')]
Combination of list in sorted order(with replacement) is:
[(4, 4, 4), (4, 4, 2), (4, 2, 2), (2, 2, 2)]
Combination of container in sorted order(with replacement) is:
[(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)]
Terminating Iterator
Terminating iterators are typically employed to operate on a limited input sequence, producing output that reflects the functionality of the method implemented within the iterator.
Various categories of terminating iterators exist:
1. accumulate(iter, func)
This function requires two parameters: the initial parameter is an iterable, while the second is a function that will be executed during each iteration over the values within the iterable. In the absence of a specified function in the accumulate iterator, the default operation performed will be addition. The resulting iterable is contingent upon the input iterable; therefore, if the input iterable lacks any values, the output iterable will likewise be devoid of content.
Python accumulate(iter, func) Iterator:
To demonstrate the accumulate iterator in Python, let's consider an example.
import itertools
import operator
# initializing list 1
list1 = [1, 4, 5, 7, 9, 11]
# using accumulate() that will prints the successive summation of elements
print("The sum is : ", end="")
print(list(itertools.accumulate(list1)))
# using accumulate() that will prints the successive multiplication of elements
print("The product is : ", end="")
print(list(itertools.accumulate(list1, operator.mul)))
# using accumulate() that will prints the successive summation of elements
print("The sum is : ", end="")
print(list(itertools.accumulate(list1)))
# using accumulate() that will print the successive multiplication of elements
print("The product is : ", end="")
print(list(itertools.accumulate(list1, operator.mul)))
Output:
The sum is : [1, 5, 10, 17, 26, 37]
The product is : [1, 4, 20, 140, 1260, 13860]
The sum is : [1, 5, 10, 17, 26, 37]
The product is : [1, 4, 20, 140, 1260, 13860]
2. chain(iter1, iter2)
It is utilized to output every value within an iterable provided as a series and specified in the arguments.
Python chain(iter1, iter2) Example:
Examine the subsequent illustration to showcase the chain(iter1, iter2) iterator available in Python.
import itertools
# declaring list 1
list1 = [1, 2, 3, 4]
# declaring list 2
list2 = [1, 5, 6, 8]
# declaring list 3
list3 = [9, 10, 11, 12]
# using the chain() function that will print all elements of the lists
print("The output is : ", end="")
print(list(itertools.chain(list1, list2, list3)))
Output:
The output is: [1, 2, 3, 4, 1, 5, 6, 8, 9, 10, 11, 12]
3. dropwhile(func, seq)
The character begins to print only subsequent to the execution of the func.
Python dropwhile(func, seq) Example:
Consider the following argument:
import itertools
# initializing list
list1 = [2, 4, 5, 7, 8]
# using dropwhile() iterator that will start displaying after the condition is false
print("The output is : ", end="")
print(list(itertools.dropwhile(lambda x: x % 2 == 0, list1)))
Output:
The output is : [5, 7, 8]
4. filterfalse(func,seq)
Based on its designation, we can infer that this iterator outputs only the values for which the provided function yields a false result.
Python filterfalse(func, seq) Example:
Examine the subsequent illustration to illustrate the filterfalse(func, seq) iterator in Python.
import itertools
# declaring list
list1 = [12, 14, 15, 27, 28]
# using filterfalse() iterator that will print false values
print("The Output is: ", end="")
print(list(itertools.filterfalse(lambda x: x % 2 == 0, list1)))
Output:
The Output is : [15, 27]
5. islice(iterable,start,stop,step)
It partitions the specified iterable based on the provided indices. It takes four parameters in order: iterable, container, starting position, ending position, and an optional step.
The function islice in Python can be utilized as follows: islice(iterable, start, stop, step). For instance:
Examine the subsequent example to illustrate the islice iterator in Python.
import itertools
# Declaring list
list1 = [12, 34, 65, 73, 80, 19, 20]
# using islice() iterator that will slice the list acc. to given argument
# starts printing from 3nd index till 8th skipping 2
print("The sliced list values are : ", end="")
print(list(itertools.islice(list1, 2, 8, 2)))
Output:
The sliced list values are : [34, 73, 19]
6. starmap(func, tuple list)
The function requires two parameters: the initial parameter is a function, while the second parameter is a list containing elements formatted as tuples.
Python starmap(func, tuple list) Example:
Examine the subsequent example to illustrate the starmap(func, tuple_list) iterator in Python.
import itertools
# Declaring list that contain tuple as element
list1 = [(10, 20, 15), (18, 40, 19), (53, 42, 90), (16, 12, 27)]
# using starmap() iterator for selection value acc. to function
# selects max of all tuple values
print("The values acc. to function are : ", end="")
print(list(itertools.starmap(max, list1)))
Output:
The values acc. to function are : [20, 40, 90, 27]
7. takewhile(func, iterable)
It serves as the opposite of dropwhile. It will output values until a false condition is encountered.
Python takewhile(func, iterable) Example:
Examine the subsequent illustration to showcase the takewhile(func, iterable) iterator in Python.
import itertools
# Defining a list
list1 = [20, 42, 64, 77, 8, 10, 20]
# takewhile() iterator is used to print values till condition return false.
print("Print until 1st false value returned : ", end="")
print(list(itertools.takewhile(lambda x: x % 2 == 0, list1)))
Output:
The list values until false value return : [20, 42, 64]
8. tee(iterator, count)
It partitions the container into multiple iterators, as specified in the argument.
Python tee(iterator, count) Example:
Examine the subsequent illustration to showcase the tee(iterator, count) iterator in Python.
import itertools
# Declaring list
li = [1, 2, 3, 4, 5, 6, 7]
# storing list in iterator
iti = iter(li)
# using tee() iterator to create a list of iterators
# Creating list of 3 iterators having similar values.
it = itertools.tee(iti, 3)
# It will print object of iterator
print(it)
print("The iterators are : ")
for i in range(0, 2):
print(list(it[i]))
Output:
(<itertools._tee object at 0x01B88D88>, <itertools._tee object at 0x01B88DA8>, <itertools._tee object at 0x01B88BA8>)
The iterators are :
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]
9. zip_longest(iterable1, iterable2, fillval)
It outputs the values of the iterable in an alternating sequence. If one of the iterables exhausts all its values, the remaining positions are populated with the designated fill value.
Python zip_longest Example:
Examine the subsequent example to illustrate the zip_longest iterator in Python.
import itertools
print(" The combined value of iterrables is :")
print(*(itertools.zip_longest('Python', 'Example', fillvalue='_')))
Output:
The combined value of iterables is :
('J', 'T') ('a', 'p') ('v', 'o') ('a', 'i') ('_', 'n') ('_', 't')
Conclusion
In this tutorial, we have explored a range of valuable iterators in conjunction with the itertools module. We discovered that itertools is among the most impressive standard libraries in Python 3. This library offers a variety of remarkable functions. Python supplies comprehensive documentation for itertools. We examined different categories of iterators within the itertools module, including infinite iterators, combinatorial iterators, and terminating iterators. Additionally, we reviewed the applications of several functions such as tee, takewhile, starmap, dropwhile, and chain.
Python Itertools FAQs
1. What is itertools in Python?
Itertool is among the top libraries in Python 3. This library includes some impressive functions, supported by comprehensive documentation of itertools. A crucial aspect of itertools is that its functions are designed to create code that is both memory-efficient and highly accurate.
2. How do you use itertools.count?
The count function can be utilized, comprising both a starting value and a step increment.
Consider an illustration where we will output even numbers beginning at 10 and extending to 20, utilizing a step value of 2.
#importing the itertools
#importing the itertools module
import itertools
#using itertools.count() function with start value 10 and step value 2
for i in itertools.count(10, 2):
if i > 20:
break
print(i)
Output:
10
12
14
16
18
20
3. What does itertools.cycle do?
The itertools.cycle function iterates over the elements of an iterable in an endless loop. Here’s an illustration of the itertools.cycle function:
Example:
#importing the itertools module
import itertools
#initializing the count to 0
count = 0
for item in itertools.cycle(["T", "point", "Tech"]):
print(item)
count += 1
if count == 6:
break
Output:
T
point
Tech
T
point
Tech
4. How does itertools.chain work?
The itertools.chain function merges several iterables into one continuous sequence. Consider the following example:
Example:
#importing the itertools module
import itertools
a = ['T', 'point']
b = ['Tech', 'PvtLtd']
#printing the output
print(list(itertools.chain(a, b)))
Output:
['T', 'point', 'Tech', 'PvtLtd']
5. Why is itertools useful?
The itertools is useful for various purposes, such as:
- It saves memory
- It is efficient in looping
- It is handy for combinatorics, which means Permutations and Combinations
- It is beneficial in data analysis, AI/ML, etc.