{"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

chain (*iterables)<\/pre>\n

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

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

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

Given first List = [9, 8, 7, 6]\r\nGiven second List = [40, 30, 20, 10]<\/pre>\n

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

The result after chaining the given two lists:\r\n[9, 8, 7, 6, 40, 30, 20, 10]<\/pre>\n

Explanation:<\/strong><\/p>\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

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

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

Given first string = \"hello\"\r\nGiven second string = \"ALL\"<\/pre>\n

Output:<\/strong><\/p>\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