Python - Itertools.compress() Last Updated : 19 Feb, 2020 Comments Improve Suggest changes Like Article Like Report Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Compress() The itertools.compress() falls under the category of terminating iterators. It means that these iterators are used to work on the short input sequences and produce the output based on the functionality of the method used. Compress(): This iterator selectively picks the values to print from the passed container according to the boolean list value passed as other arguments. The arguments corresponding to boolean true are printed else all are skipped. In this, we give two parameters to the function. The first parameter will the iterator and the second parameter will be a selector either True/1 or False/0. If the first parameter's corresponding selector is True, then the corresponding data will be printed and we will get the output accordingly. Syntax: compress(iter, selector) Example 1 : Python3 1== # Python code to demonstrate the working of # compress() import itertools import operator Codes =['C', 'C++', 'Java', 'Python'] selectors = [False, False, False, True] Best_Programming = itertools.compress(Codes, selectors) for each in Best_Programming: print(each) Output: Python In the above code, in the Codes list, we have stored four variables and in the selectors' list, we have four boolean values. When we use the itertools.compress() then the value False is assigned to 'C', False to 'C++', False to 'Java' and True to 'Python'. Now while iterating through the loop we will get the output to which the value True is assigned. So, we get only "Python" while iterating 'Best Programming'. Example 2 : Python3 1== # Python code to demonstrate the working of # compress() import itertools import operator example = itertools.compress('ABCDE', [1, 0, 1, 0, 0]) for each in example: print(each) Output: A C Comment More infoAdvertise with us Next Article Python - Itertools.dropwhile() A Akshaysharma11 Follow Improve Article Tags : Python Python-itertools Practice Tags : python Similar Reads Python Itertools Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and we wa 12 min read Python - Itertools.count() Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it co 3 min read Python - Itertools.cycle() Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators 3 min read Python - itertools.repeat() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re 2 min read Itertools.accumulate()-Python itertools.accumulate() is an iterator that takes two arguments, an iterable (target) and an optional function. The function is applied at each iteration to accumulate the result. By default, if no function is provided, it performs addition. If the input iterable is empty, the output will also be emp 3 min read Python - Itertools.chain() The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is 4 min read Python - Itertools.chain.from_iterable() Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Th 2 min read Python - Itertools.compress() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co 2 min read Python - Itertools.dropwhile() Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more inform 1 min read Python - Itertools.filterfalse() In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() funct 2 min read Like