Link Search Menu Expand Document

Python Iterations using Libraries

Definite iteration loops are commonly known as for loops since the keyword ‘for’ is used to introduce them in almost all computer languages, including Python. Historically, programming languages included several variants of them for a loop.

A simple numeric range expression with start and end values is the most basic for loop. The detailed presentation varies depending on the language, but it often looks like the sample below.

for i = 1 to 10

The loop’s body runs ten times. On the first iteration, the user set variable I to 1, 2 on the second. This type of for loop is in the programming languages BASIC, Algol, and Pascal.

Python has introduced a Library that creates iterators for efficient looping.

IterTools

ITertool is a Python package that provides several functions that work on iterators to generate complicated iterators. This module functions as a fast, memory-efficient tool that works alone or combined to construct iterator algebra.

Assume there are two lists, and the user needs to multiply the elements of both of them. The basic strategy, which involves iterating through both lists concurrently and multiplying them, can be used. The other method uses the map function, supplying the ‘mul’ operator as the first parameter and Lists as the second and third parameters. Let’s examine how much time each method takes.

# Python program to demonstrate
# iterator module
import operator
import time
# Defining lists
L1 = [1, 2, 3]
L2 = [2, 3, 4]
# Starting time before map
# function
t1 = time.time()
# Calculating result
a, b, c = map(operator.mul, L1, L2)
# Ending time after map
# function
t2 = time.time()
# Time taken by map function
print("Result:", a, b, c)
print("Time taken by map function: %.6f" %(t2 - t1))

Results for both techniques would be the same ‘2 6 12’; however, the time taken by map function is 0.00005 and for loop took 0.00014. The time required by the map function is half of the time required by the for a loop. It demonstrates that iterTools are a quick and memory-efficient tool.

There are three types of IterTools.

  • Infinite Iterators
  • Combinatoric iterators
  • Terminating iterators

Infinite Iterators

In Python, an iterator is any Python type that works with a ‘for in loop.’ Python’s built-in iterators include lists, tuples, dictionaries, and sets. However, it is not always required for an iterator object to exhaust; it can sometimes be endless. Infinite iterators are the name given to this sort of iterator.

Count

An infinite sequence of evenly spaced values, for example, can be generated by the user:

start = 10
stop = 1
my_counter = it.count(start, stop)
for i in my_counter:
    # this loop will run for ever
    print(i)

Combinatoric Iterators

Combinatoric iterators are recursive generators for simplifying combinatorial constructs like permutations, combinations, and Cartesian products.

Product

The cartesian product of input iterable computes with this tool. The user uses the optional repeat keyword argument to specify the number of repetitions to compute the product of an iterable with itself. This function returns a list of tuples in sorted order.

product(iterables*, repeat=1)

Terminating Iterators

Terminating iterators are used to work on short input sequences and provide an output based on the method’s capability. These functionalities can work for a variety of purposes, including

The user may have several iterations, and the user wants to act on each iterable’s elements one by one in a single sequence.

Or when the user wants to remove elements from the iterable while the predicate is factual and then perform an action on the remaining elements.

Chain

This method creates an iterator that returns elements from all input iterables in sequential order until no more elements are available. As a result, it can treat multiple sequences as if they were a single sequence.

chain = it.chain([1,2,3], ['a','b','c'], ['End'])
for i in chain:
    print(i)

Other useful articles:


Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy