{"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 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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Itertools Module: 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. Itertools.chain.from_iterable() Function: chain.from_iterable() belongs to the category of terminating iterators. This function takes a single iterable as an argument, and all of the elements …<\/p>\nchain.from_iterable(iterable)<\/pre>\n
Given List = [\"good\", \"morning\", \"all\"]<\/pre>\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
Given List = [\"hello\", ['a', 'l', 'l']]<\/pre>\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
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"good\", \"morning\", \"all\"]\r\n# Pass the given list as an argument to the itertools.chain.from_iterable()\r\n# function that returns a flattened list (list of characters) containing all\r\n# of the given list elements.\r\n# Store it in another variable.\r\nrslt_chain = itertools.chain.from_iterable(gvn_lst)\r\n# Convert the above result into a list using the list() function and store it\r\n# in a variable.\r\nrslt_lst = list(rslt_chain)\r\n# Print the flattened list of the given list.\r\nprint(\"The flattened list of the given list:\")\r\nprint(rslt_lst)\r\n<\/pre>\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
string and List as one input at a time<\/h5>\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"hello\", ['a', 'l', 'l']]\r\n# Pass the given list as an argument to the itertools.chain.from_iterable()\r\n# function that returns a flattened list (list of characters) containing all\r\n# of the given list elements.\r\n# Store it in another variable.\r\nrslt_chain = itertools.chain.from_iterable(gvn_lst)\r\n# Convert the above result into a list using the list() function and store it\r\n# in a variable.\r\nrslt_lst = list(rslt_chain)\r\n# Print the flattened list of the given list.\r\nprint(\"The flattened list of the given list:\")\r\nprint(rslt_lst)\r\n<\/pre>\n
The flattened list of the given list:\r\n['h', 'e', 'l', 'l', 'o', 'a', 'l', 'l']<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the list as user input using list(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(input(\r\n 'Enter some random List Elements separated by spaces = ').split())\r\n\r\n# Pass the given list as an argument to the itertools.chain.from_iterable()\r\n# function that returns a flattened list (list of characters) containing all\r\n# of the given list elements.\r\n# Store it in another variable.\r\nrslt_chain = itertools.chain.from_iterable(gvn_lst)\r\n# Convert the above result into a list using the list() function and store it\r\n# in a variable.\r\nrslt_lst = list(rslt_chain)\r\n# Print the flattened list of the given list.\r\nprint(\"The flattened list of the given list:\")\r\nprint(rslt_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = python code\r\nThe flattened list of the given list:\r\n['p', 'y', 't', 'h', 'o', 'n', 'c', 'o', 'd', 'e']<\/pre>\n","protected":false},"excerpt":{"rendered":"