Python Itertools
Itertools in Python
In the vast world of Python programming, one module stands out as a hidden gem, providing a multitude of tools to simplify your coding tasks and enhance your efficiency – `itertools`. In this page, we will take you on a journey through the capabilities of itertools in Python, from its basics to more advanced usage, revealing how it can be a game-changer for developers.
Python Itertools
Itertools is a Python module that provides a collection of powerful, memory-efficient tools for working with iterators and iterable objects. The itertools module in Python is a powerful library that provides various functions and iterators for working with iterators and combinatorial operations.To use it, you need to import it at the beginning of your Python script like this:
import itertools
Commonly used Functions and Iterators from Itertools Module
Functions and Iterators | Working |
---|---|
count(start=0, step=1) | Returns an infinite iterator that generates numbers starting from start and incrementing by step in each iteration. |
cycle(iterable) | Returns an iterator that cycles through elements of the input iterable indefinitely. |
repeat(elem, times=None) | Returns an iterator that repeats the element ‘elem’ indefinitely, or ‘times’ times if specified. |
chain(iterable1, iterable2, …) | Combines multiple iterables into one long iterator. |
combinations(iterable, r) | Generates all possible combinations of r elements from the input iterable. |
permutations(iterable, r) | Generates all possible permutations of r elements from the input iterable. |
product(iterable1, iterable2, …) | Computes the Cartesian product of input iterables. |
groupby(iterable, key=None) | Groups consecutive elements of an iterable based on a key function. |
islice(iterable, start, stop, step) | Returns a slice of the iterable from start to stop, with a specified step. |
tee(iterable, n=2) | Splits an iterable into n independent iterators. |
zip_longest(iterable1, iterable2, …, fillvalue=None) | Combines multiple iterables like zip, but fills in missing values with the specified fillvalue |
Itertools is a versatile Python module that simplifies complex operations on iterable objects. Its efficient tools can significantly boost your productivity and help you write more elegant code.
Let’s see an examples of using various itertools functions and iterators to perform different operations on sequences and iterators:
Code :
import itertools count_iterator = itertools.count(start=0, step=1) cycle_iterator = itertools.cycle(['A', 'B', 'C']) repeat_iterator = itertools.repeat('X', times=3) chain_iterator = itertools.chain([1, 2, 3], [4, 5, 6]) combinations = itertools.combinations([1, 2, 3, 4], 2) permutations = itertools.permutations([1, 2, 3], 3) product = itertools.product(['a', 'b'], [1, 2, 3]) data = [(1, 'A'), (2, 'A'), (3, 'B'), (4, 'B'), (5, 'B')] grouped = itertools.groupby(data, key=lambda x: x[1]) sliced = itertools.islice(range(10), 2, 8, 2) iter1, iter2 = itertools.tee([10, 20, 30, 40, 50]) zipped = itertools.zip_longest([1, 2, 3], ['A', 'B'], fillvalue=None) print(list(itertools.islice(count_iterator, 5))) print(list(itertools.islice(cycle_iterator, 7))) print(list(repeat_iterator)) print(list(chain_iterator)) print(list(combinations)) print(list(permutations)) print(list(product)) for key, group in grouped: print(key, list(group)) print(list(sliced)) print(list(iter1), list(iter2)) print(list(zipped))
Output :
[0, 1, 2, 3, 4]
['A', 'B', 'C', 'A', 'B', 'C', 'A']
['X', 'X', 'X']
[1, 2, 3, 4, 5, 6]
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3)]
A [(1, 'A'), (2, 'A')]
B [(3, 'B'), (4, 'B'), (5, 'B')]
[2, 4, 6]
[10, 20, 30, 40, 50] [10, 20, 30, 40, 50]
[(1, 'A'), (2, 'B'), (3, None)]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment