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
It is a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output. Its output cannot be used directly and thus explicitly converted into iterables. This function come under the category iterators terminating iterators.
The iternal working of chain can be implemented as given below :
def chain(*iterables): for it in iterables: for each in it: yeild (each)
Example 1: The odd numbers and even numbers are in separate lists. Combine them to form a new single list.
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Example 2: Some of the consonants are in a list. The vowels are given in a list. Combine them and also sort them.
['a', 'd', 'e', 'f', 'i', 'k', 'l', 'n', 'o', 'p', 'u']
Example 3: In the example below, Each String is considered to be an iterable and each character in it is considered to be an element in the iterator. Thus every character is yielded
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
before joining : [‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’]
After joining : GeeksforGeeks
It is similar to chain, but it can be used to chain items from a single iterable. The difference is demonstrated in the example given below:
using chain : [‘ABC’, ‘DEF’, ‘GHI’, ‘JKL’]
using chain.from_iterable : [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’]
Example 6: Now consider a list like the one given below :
li=['123', '456', '789']
You are supposed to calculate the sum of the list taking every single digit into account. So the answer should be :
1+2+3+5+6+7+8+9 = 45
This can be achieved easily using the code below :
res = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] new_res = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 45
To simplify it, we combine the steps. An optimized approach is given below:
res = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 45
- Python - Read blob object in python using wand library
- Python | Convert list to Python array
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- twitter-text-python (ttp) module - Python
- MySQL-Connector-Python module in Python
- Python | Merge Python key values to list
- Important differences between Python 2.x and Python 3.x with examples
- Python | PRAW - Python Reddit API Wrapper
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Visualizing O(n) using Python
- Python vs PHP
- Python Set | pop()
- abs() in Python
- gcd() in Python
- Python PIP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.