Python Itertools.compress() Function with Examples

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 output based on the functionality of the method.

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.

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.

 

Syntax:

compress(iterator, selector)

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'btechgeeks']
Given selectors List = [True, False, False, True]

Output:

hello
btechgeeks

Explanation:

Here, In the selection list, first element is True. so, it prints the
respective index element from the given list 'hello'.
If it is False, respective index element from the given list gets skipped.
Therefore it prints only 'hello' and 'btechgeeks' from given list.

Example2:

Input:

Given String = "hello"
Given selectors List = [0, 1, 0, 1, 1]

Output:

e
l
o

Itertools.compress() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import itertools module using the import keyword.
  • Import operator module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the selectors list as static input and store it in another variable.
  • Pass the given list and selectors list as the arguments to the itertools.compress() function where If boolean True in selectors list, the respective arguments in the given list are printed; otherwise, all are skipped.
  • Store it in another variable.
  • Iterate in the above result iterable using the for loop.
  • Inside the loop, print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Import operator module using the import keyword.
import operator

# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks']
# Give the selectors list as static input and store it in another variable.
gvn_selectrs = [True, False, False, True]
# Pass the given list and selectors list as the arguments to the itertools.compress()
# function where If boolean True in selectors list, the respective arguments in the
# given list are printed; otherwise, all are skipped.
# Store it in another variable.
rslt = itertools.compress(gvn_lst, gvn_selectrs)
# Iterate in the above result iterable using the for loop.
for itr in rslt:
    # Inside the loop, print the iterator value.
    print(itr)

Output:

hello
btechgeeks
Using 1, 0 Boolean values:
# Import itertools module using the import keyword.
import itertools
# Import operator module using the import keyword.
import operator

# Give the string as static input and store it in a variable.
gvn_str = "hello"
# Give the selectors list as static input and store it in another variable.
gvn_selectrs = [0, 1, 0, 1, 1]
# Pass the given str and selectors list as the arguments to the itertools.compress()
# function where If boolean True(1) in selectors list, the respective arguments in the
# given string are printed; otherwise, all are skipped.
# Store it in another variable.
rslt = itertools.compress(gvn_str, gvn_selectrs)
# Iterate in the above result iterable using the for loop.
for itr in rslt:
    # Inside the loop, print the iterator value.
    print(itr)

Output:

e
l
o

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import itertools module using the import keyword.
  • Import operator module using the import keyword.
  • Give the list as user input using list(), input(),and split() functions.
  • Store it in a variable.
  • Give the selectors list as user input using list(), input(),and split() functions.
  • Store it in another variable.
  • Converting to boolean data type
  • Loop in the given selectors list using the for loop
  • If the iterator value of the given selectors list is ‘True’, then modify the list item with the boolean True.
  • Else modify the list item with the boolean False.
  • Pass the given list and selectors list as the arguments to the itertools.compress() function where If boolean True in selectors list, the respective arguments in the given list are printed; otherwise, all are skipped.
  • Store it in another variable.
  • Iterate in the above result iterable using the for loop.
  • Inside the loop, print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the selectors list as user input using list(),input(),and split() functions.
# Store it in a variable.
gvn_selectrs = list(input(
   'Enter some random List Elements separated by spaces = ').split())
#converting to boolean data type
# Loop in the given selectors list using the for loop
for i in range(len(gvn_selectrs)):
    # If the iterator value of the given selectors list is 'True',then 
    # modify the list item with the boolean True.
    if(gvn_selectrs[i]=='True'):
        gvn_selectrs[i]=True
    # Else modify the list item with the boolean False.
    else:
        gvn_selectrs[i]=False
       

# Pass the given list and selectors list as the arguments to the itertools.compress()
# function where If boolean True in selectors list, the respective arguments in the
# given list are printed; otherwise, all are skipped.
# Store it in another variable.
rslt = itertools.compress(gvn_lst, gvn_selectrs)
# Iterate in the above result iterable using the for loop.
for itr in rslt:
    # Inside the loop, print the iterator value.
    print(itr)

Output:

Enter some random List Elements separated by spaces = 10 20 30
Enter some random List Elements separated by spaces = True False True
10
30