{"id":25577,"date":"2021-11-23T08:55:26","date_gmt":"2021-11-23T03:25:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=25577"},"modified":"2021-11-23T08:55:26","modified_gmt":"2021-11-23T03:25:26","slug":"python-itertools-compress-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-compress-function-with-examples\/","title":{"rendered":"Python Itertools.compress() 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.compress() Function:<\/strong><\/p>\n Itertools.compress() belongs to the class of terminating iterators. This means that these iterators are used to process short input sequences and generate output based on the functionality of the method.<\/p>\n This iterator selects the values to print from the passed container based on the boolean list value passed as an additional argument. If boolean true, the arguments are printed; otherwise, all are skipped.<\/p>\n We give the function two parameters in this case. The first parameter is an iterator, and the second is a selector that can be True\/1 or False\/0. If the first parameter’s corresponding selector is True, the corresponding data will be printed, and the output will reflect this.<\/p>\n <\/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 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.compress() Function: Itertools.compress() belongs to the class of terminating iterators. This means that these iterators are used to process short input sequences and generate …<\/p>\ncompress(iterator, selector)<\/pre>\n
Given List = ['hello', 'this', 'is', 'btechgeeks']\r\nGiven selectors List = [True, False, False, True]<\/pre>\n
hello\r\nbtechgeeks<\/pre>\n
Here, In the selection list, first element is True. so, it prints the\r\nrespective index element from the given list 'hello'.\r\nIf it is False, respective index element from the given list gets skipped.\r\nTherefore it prints only 'hello' and 'btechgeeks' from given list.<\/pre>\n
Given String = \"hello\"\r\nGiven selectors List = [0, 1, 0, 1, 1]<\/pre>\n
e\r\nl\r\no<\/pre>\n
Itertools.compress() 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# Import operator module using the import keyword.\r\nimport operator\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = ['hello', 'this', 'is', 'btechgeeks']\r\n# Give the selectors list as static input and store it in another variable.\r\ngvn_selectrs = [True, False, False, True]\r\n# Pass the given list and selectors list as the arguments to the itertools.compress()\r\n# function where If boolean True in selectors list, the respective arguments in the\r\n# given list are printed; otherwise, all are skipped.\r\n# Store it in another variable.\r\nrslt = itertools.compress(gvn_lst, gvn_selectrs)\r\n# Iterate in the above result iterable using the for loop.\r\nfor itr in rslt:\r\n # Inside the loop, print the iterator value.\r\n print(itr)\r\n<\/pre>\n
hello\r\nbtechgeeks<\/pre>\n
Using 1, 0 Boolean values:<\/h5>\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Import operator module using the import keyword.\r\nimport operator\r\n\r\n# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello\"\r\n# Give the selectors list as static input and store it in another variable.\r\ngvn_selectrs = [0, 1, 0, 1, 1]\r\n# Pass the given str and selectors list as the arguments to the itertools.compress()\r\n# function where If boolean True(1) in selectors list, the respective arguments in the\r\n# given string are printed; otherwise, all are skipped.\r\n# Store it in another variable.\r\nrslt = itertools.compress(gvn_str, gvn_selectrs)\r\n# Iterate in the above result iterable using the for loop.\r\nfor itr in rslt:\r\n # Inside the loop, print the iterator value.\r\n print(itr)\r\n<\/pre>\n
e\r\nl\r\no<\/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(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the selectors list as user input using list(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_selectrs = list(input(\r\n 'Enter some random List Elements separated by spaces = ').split())\r\n#converting to boolean data type\r\n# Loop in the given selectors list using the for loop\r\nfor i in range(len(gvn_selectrs)):\r\n # If the iterator value of the given selectors list is 'True',then \r\n # modify the list item with the boolean True.\r\n if(gvn_selectrs[i]=='True'):\r\n gvn_selectrs[i]=True\r\n # Else modify the list item with the boolean False.\r\n else:\r\n gvn_selectrs[i]=False\r\n \r\n\r\n# Pass the given list and selectors list as the arguments to the itertools.compress()\r\n# function where If boolean True in selectors list, the respective arguments in the\r\n# given list are printed; otherwise, all are skipped.\r\n# Store it in another variable.\r\nrslt = itertools.compress(gvn_lst, gvn_selectrs)\r\n# Iterate in the above result iterable using the for loop.\r\nfor itr in rslt:\r\n # Inside the loop, print the iterator value.\r\n print(itr)<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30\r\nEnter some random List Elements separated by spaces = True False True\r\n10\r\n30<\/pre>\n","protected":false},"excerpt":{"rendered":"