Updated March 28, 2023
Introduction to Python Itertools
Python Itertools are tools that are helpful in achieving memory efficiency and which can be very useful if implied individually or as a combination. The functions provided here are very similar to the functions of SML and Haskell kinds of languages. Speedy performance and efficient memory processing are the two key capabilities of these Itertools,
Why Python itertools?
One of the key reasons for using itertools is because of its capability to use memory very efficiently. The iter-based tools do not hold any large memory at one particular time because the data which is going to be produced is not kept in memory until processing which automatically decreases the memory usage capability and will relatively reduce the issues like large datasets and performance issues.
How do Itertools Work?
Basically, the concept of iterations are classified into three types; they are as follows,
- Infinite Iterators
- Iterators that terminate on the short input sequence
- Combinatoric Iterators
Functions or methods which fall under each of these iterators are mentioned with examples below,
1. Infinite Iterators
Iterator types like lists, tuples, dictionaries, and sets do not necessarily need to exhaust, which means in some instances, it can be looped on infinitely. These kinds of methods that produce infinite iterations are namely called as infinite iterators,
The count() function mentions the number of times a particular item occurs in the iterable.
List = [1,4,2,9,7,8,9,3,1] print("List Verified:",List) print("Count of value '9' in the list:",List.count(9))
The cycle() function repeats the given set of iterable continuously and seamlessly.
import itertools Iter_List = [1,2,4] result = itertools.cycle(Iter_List) count = 0 print("First three Output Cycle Printed:") for value in result: count = count + 1 if count < 10: print(value) else: break
Itertools.repeat() is used to repeat a given value n number of times.
import itertools Iter_List = [1,2,4] print("Print the above Iter_List 3 times") result = itertools.repeat(Iter_List,3) count = 0 for value in result: count = count + 1 if count < 10: print(value) else: break
2. Iterators that terminate on a short input sequence:
These are conditions where two are more iterators combine to form a single iterator on some specific logic. If one among the input iterators is smaller, the iterators will not break as soon as the shortest input gets exhausted.
The chain() method is used to combine two iterable together into a single iterable entity.
import itertools Iter1 = [4,2,4] Iter2 = [7,1,6,8] print("Keyed In Input List1:",Iter1) print("Keyed In Input List2:",Iter2) Chained_List = list(itertools.chain(Iter1,Iter2)) print("Chained List:",Chained_List)
The compress() method is used to selectively pick values based on the boolean values passed through one argument.
import itertools Iter1 = ['Istanbul','Karachi','Colombo','Mumbai','Delhi','California'] Iter2 = [0,0,0,1,True,False] print("Input List",Iter1) Compressed_List = list(itertools.compress(Iter1,Iter2)) print("Cities in India:",Compressed_List)
The dropwhile() returns a list of iter values that fall after the first time the condition fails. The takewhile() returns a list of iter values that fall before the first time the condition fails.
import itertools Iter_values = [1,3,45,2,4,6,78,89,0,1,3,4,555,4] dropwhile_List = list(itertools.dropwhile(lambda iter: iter < 10,Iter_values)) takewhile_List = list(itertools.takewhile(lambda iter: iter < 10,Iter_values)) print("Values not dropped:",dropwhile_List) print("Values not taken:",takewhile_List)
The filterfalse() filters those iterators which do not satisfy the given condition.
import itertools Iter_values = [1,3,45,2,4,6,78,89,0,1,3,4,555,4] print("Iter Verified:",Iter_values) filterfalse_List = list(itertools.filterfalse(lambda iter: iter < 10,Iter_values)) print("False values (Iters greater than 10):",filterfalse_List)
Slices the mentioned range of iterator values from the iterator list using the mentioned index.
import itertools Iter_values = [1,3,45,2,4,6,78,89,0,1,3,4,555,4] print("Iter Involved:",Iter_values) filterfalse_List = list(itertools.islice(Iter_values,2,7)) print("Iter sliced by position:",filterfalse_List)
3. Combinatoric Iterators
The Combinatoric Iterators are responsible for performing combinations, permutations and calculating the Cartesian products. The four major combinatoric Iterators are as below,
The product() method is used for calculating the cartesian value of the iterable entities.
import itertools Iter_values = [1,3,5] print("Iter Involved:",Iter_values) Cartesian_List = list(itertools.product(Iter_values,'2')) print("Cartesian Output:",Cartesian_List)
The combinations() method is used to generate all possible combinations for the given iter.
import itertools Iter_values = [1,3,5] print("Iter Involved:",Iter_values) Cartesian_List = list(itertools.combinations(Iter_values,3)) print("Cartesian Output:",Cartesian_List)
The zip() function zips two individual, collective datatype items into a single tuple. This means the first items in both elements form the first tuple item in the resultant tuple; similarly, the second item in both elements form the second tuple in the resultant tuple, and the tuple formation goes on. when the length of the datatype items differ, then the element with the least length decides the length of the end tuple
Set1= (1, 2) Set2 = ("India", "China", "Pakistan") print("Value in set1:",Set1) print("Value in set2:",Set2) Output_Set = zip(Set1,Set2) print("Output Set:",set(Output_Set))
The map function is used for executing a mentioned function for each and every element in the iterable.
def myfunc(a, b): return a + b x = map(myfunc, (1,2,3), (4,5,6)) print(list(x))
If you want to come across sprint of successive numbers, letters, dates, booleans or several erstwhile orderable objects, Consecutive groups which fall under more_itertools are the best possible solution on this. The below mentione example has a large list of dates and some among the list are very much consecutive, So when some among them are consecutive they need to be passed on to the group function. In order to pass these functions to the consecutive group section they are been initially converted into ordinal numbers and then a compreshension is implied upon it to get them iterated. then we use ordinal dates to iterate upon the given list of consecutive_groups.
import datetime import more_itertools dates = [ datetime.datetime(2019, 2, 1), datetime.datetime(2019, 3, 1), datetime.datetime(2019, 4, 12), datetime.datetime(2019, 1, 3), datetime.datetime(2019, 4, 2), datetime.datetime(2019, 2, 4) ] Actual_dates =  for iter in dates: Actual_dates.append(iter.toordinal()) groups = [list(map(datetime.datetime.fromordinal, group)) for group in more_itertools.consecutive_groups(Actual_dates)] print(groups)
Itertools is definitely one of the most powerful tools in the python arena. They help to produce a large instance of memory efficiency and programmatic logic reduction.
This is a guide to Python Itertools. Here we discuss that the Python Itertools are the most powerful tools in the python arena, along with examples. You may also have a look at the following articles to learn more –