{"id":25575,"date":"2021-11-23T08:55:37","date_gmt":"2021-11-23T03:25:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=25575"},"modified":"2021-11-23T08:55:37","modified_gmt":"2021-11-23T03:25:37","slug":"python-itertools-chain-from_iterable-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-chain-from_iterable-function-with-examples\/","title":{"rendered":"Python Itertools.chain.from_iterable() Function with Examples"},"content":{"rendered":"

Itertools Module:<\/strong><\/p>\n

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.<\/p>\n

Itertools.chain.from_iterable() Function:<\/strong><\/p>\n

chain.from_iterable() belongs to the category of terminating iterators. This function takes a single iterable as an argument, and all of the elements of the input iterable must also be iterable, and it returns a flattened iterable containing all of the input iterable’s elements.<\/p>\n

Syntax:<\/strong><\/p>\n

chain.from_iterable(iterable)<\/pre>\n

Parameters<\/strong><\/p>\n

iterable:<\/strong> It could be any iterable like list, string, and so on.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List = [\"good\", \"morning\", \"all\"]<\/pre>\n

Output:<\/strong><\/p>\n

The flattened list of the given list:\r\n['g', 'o', 'o', 'd', 'm', 'o', 'r', 'n', 'i', 'n', 'g', 'a', 'l', 'l']<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List = [\"hello\", ['a', 'l', 'l']]<\/pre>\n

Output:<\/strong><\/p>\n

The flattened list of the given list:\r\n['h', 'e', 'l', 'l', 'o', 'a', 'l', 'l']<\/pre>\n

Itertools.chain.from_iterable() Function with Examples in Python<\/h2>\n