Open In App

Combinatoric Iterators in Python

Last Updated : 12 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

An iterator is an object that can be traversed through all its values. Simply put, iterators are data type that can be looped upon. Generators are iterators but as they cannot return values instead they yield results when they are executed, using the ‘yield’ function. Generators can be recursive just like functions. These recursive generators which are used to simplify combinatorial constructs such as permutations, combinations, and Cartesian products are called combinatoric iterators.

In python there are 4 combinatoric iterators:

  1. Product()

    This tool computes the cartesian product of input iterables. To compute the product of an iterable with itself, we use the optional repeat keyword argument to specify the number of repetitions. The output of this function are tuples in sorted order.

    Syntax:

    product(iterables*, repeat=1)

    Examples:




    # import the product function from itertools module
    from itertools import product
      
    print("The cartesian product using repeat:")
    print(list(product([1, 2], repeat=2)))
    print()
      
    print("The cartesian product of the containers:")
    print(list(product(['geeks', 'for', 'geeks'], '2')))
    print()
      
    print("The cartesian product of the containers:")
    print(list(product('AB', [3,4])))

    
    

    Output:

    The cartesian product using repeat:
    [(1, 1), (1, 2), (2, 1), (2, 2)]
    
    The cartesian product of the containers:
    [('geeks', '2'), ('for', '2'), ('geeks', '2')]
    
    The cartesian product of the containers:
    [('A', 3), ('A', 4), ('B', 3), ('B', 4)]
    
  2. Permutations()

    Permutations() as the name speaks for itself is used to generate all possible permutations of an iterable. All elements are treated as unique based on their position and not their values. This function takes an iterable and group_size, if the value of group_size is not specified or is equal to None then the value of group_size becomes length of the iterable.

    Syntax:

    permutations(iterables*, group_size=None)

    Example:




    # import the product function from itertools module
    from itertools import permutations
      
    print ("All the permutations of the given list is:"
    print (list(permutations([1, 'geeks'], 2)))
    print()
      
    print ("All the permutations of the given string is:"
    print (list(permutations('AB')))
    print()
      
    print ("All the permutations of the given container is:"
    print(list(permutations(range(3), 2)))

    
    

    Output:

    All the permutations of the given list is:
    [(1, 'geeks'), ('geeks', 1)]
    
    All the permutations of the given string is:
    [('A', 'B'), ('B', 'A')]
    
    All the permutations of the given container is:
    [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
    
  3. Combinations():

    This iterator prints all the possible combinations(without replacement) of the container passed in arguments in the specified group size in sorted order.

    Syntax:

    combinations(iterables*, group_size)

    Examples:




    # import combinations from itertools module
      
    from itertools import combinations
      
    print ("All the combination of list in sorted order(without replacement) is:"
    print(list(combinations(['A', 2], 2)))
    print()
      
    print ("All the combination of string in sorted order(without replacement) is:")
    print(list(combinations('AB', 2)))
    print()
      
    print ("All the combination of list in sorted order(without replacement) is:")
    print(list(combinations(range(2),1)))

    
    

    Output:

    All the combination of list in sorted order(without replacement) is:
    [('A', 2)]
    
    All the combination of string in sorted order(without replacement) is:
    [('A', 'B')]
    
    All the combination of list in sorted order(without replacement) is:
    [(0,), (1,)]
    
    
  4. Combinations_with_replacement():

    This function returns a subsequence of length n from the elements of the iterable where n is the argument that the function takes determining the length of the subsequences generated by the function. Individual elements may repeat itself in combinations_with_replacement function.

    Syntax:

    combinations_with_replacement(iterables*, n=None)

    Examples:




    # import combinations from itertools module
      
    from itertools import combinations_with_replacement
      
    print ("All the combination of string in sorted order(with replacement) is:")
    print(list(combinations_with_replacement("AB", 2)))
    print()
      
    print ("All the combination of list in sorted order(with replacement) is:")
    print(list(combinations_with_replacement([1, 2], 2)))
    print()
      
    print ("All the combination of container in sorted order(with replacement) is:")
    print(list(combinations_with_replacement(range(2), 1)))

    
    

    Output:

    All the combination of string in sorted order(with replacement) is:
    [('A', 'A'), ('A', 'B'), ('B', 'B')]
    
    All the combination of list in sorted order(with replacement) is:
    [(1, 1), (1, 2), (2, 2)]
    
    All the combination of container in sorted order(with replacement) is:
    [(0,), (1,)]
    


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads