{"id":25572,"date":"2021-11-23T08:55:49","date_gmt":"2021-11-23T03:25:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=25572"},"modified":"2021-11-23T08:55:49","modified_gmt":"2021-11-23T03:25:49","slug":"python-itertools-chain-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-chain-function-with-examples\/","title":{"rendered":"Python Itertools.chain() 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. Chain is one such itertools function ().<\/p>\n Itertools.chain() Function:<\/strong><\/p>\n It’s a function that takes a list of iterables and returns a single iterable. It combines all of the iterables and returns a single iterable as output. Its output cannot be directly used and must thus be explicitly converted into iterables. This function belongs to the iterators terminating iterators category.<\/p>\n Syntax:<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/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. Chain is one such itertools function (). Itertools.chain() Function: It’s a function that takes a list of iterables and returns a single iterable. It …<\/p>\nchain (*iterables)<\/pre>\n
Given first List = [9, 8, 7, 6]\r\nGiven second List = [40, 30, 20, 10]<\/pre>\n
The result after chaining the given two lists:\r\n[9, 8, 7, 6, 40, 30, 20, 10]<\/pre>\n
Here the chain() function combines the given first and second list and\r\nreturns a new list[9, 8, 7, 6, 40, 30, 20, 10]<\/pre>\n
Given first string = \"hello\"\r\nGiven second string = \"ALL\"<\/pre>\n
The result after chaining the given two strings:\r\n['h', 'e', 'l', 'l', 'o', 'A', 'L', 'L']<\/pre>\n
Itertools.chain() 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 first list as static input and store it in a variable.\r\nfst_lst = [9, 8, 7, 6]\r\n# Give the second list as static input and store it in another variable.\r\nscnd_lst = [40, 30, 20, 10]\r\n# Pass the given first and second lists as the arguments to the itertools.chain()\r\n# method that combines(chains) the given two lists.\r\n# Store it in another variable.\r\nrslt_chain = itertools.chain(fst_lst, scnd_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 result after chaining the given two lists.\r\nprint(\"The result after chaining the given two lists:\")\r\nprint(rslt_lst)\r\n<\/pre>\n
The result after chaining the given two lists:\r\n[9, 8, 7, 6, 40, 30, 20, 10]<\/pre>\n
For Strings<\/h4>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the first string as static input and store it in a variable.\r\nfst_str = \"hello\"\r\n# Give the second string as static input and store it in another variable.\r\nscnd_str = \"ALL\"\r\n# Pass the given first and second strings as the arguments to the itertools.chain()\r\n# method that combines(chains) the given two strings.\r\n# Store it in another variable.\r\nrslt_chain = itertools.chain(fst_str, scnd_str)\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 result after chaining the given two strings.\r\nprint(\"The result after chaining the given two strings:\")\r\nprint(rslt_lst)\r\n<\/pre>\n
The result after chaining the given two strings:\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 first list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\nfst_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the second list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\nscnd_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n \r\n# Pass the given first and second lists as the arguments to the itertools.chain()\r\n# method that combines(chains) the given two lists.\r\n# Store it in another variable.\r\nrslt_chain = itertools.chain(fst_lst, scnd_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 result after chaining the given two lists.\r\nprint(\"The result after chaining the given two lists:\")\r\nprint(rslt_lst)\r\n\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 15 20\r\nEnter some random List Elements separated by spaces = 100 200 300\r\nThe result after chaining the given two lists:\r\n[10, 15, 20, 100, 200, 300]<\/pre>\n
For Strings<\/h4>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the first string as user input using the input() function and store it in a variable.\r\nfst_str = input(\"Enter some random string = \")\r\n# Give the second string as user input using the input() function and \r\n# store it in another variable.\r\nscnd_str = input(\"Enter some random string = \")\r\n# Pass the given first and second strings as the arguments to the itertools.chain()\r\n# method that combines(chains) the given two strings.\r\n# Store it in another variable.\r\nrslt_chain = itertools.chain(fst_str, scnd_str)\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 result after chaining the given two strings.\r\nprint(\"The result after chaining the given two strings:\")\r\nprint(rslt_lst)\r\n<\/pre>\n
Enter some random string = good\r\nEnter some random string = morning\r\nThe result after chaining the given two strings:\r\n['g', 'o', 'o', 'd', 'm', 'o', 'r', 'n', 'i', 'n', 'g']<\/pre>\n","protected":false},"excerpt":{"rendered":"