The itertools
module in Python provides functions. These functions help in iterating through iterables.
The pairwise()
method in the itertools
module returns the successive overlapping pairs taken from the input iterable. The sequence of the output pairs is in the order of the elements in the iterable. This functionality was introduced in Python version 3.10.
itertools.pairwise(iterable)
This method takes a parameter, iterables
, which represents the iterable.
from itertools import pairwiselst = [1,2,3,4,5]print("Original list - ", lst)print("Successive overlapping pairs - ", list(pairwise(lst)))string = "hello educative"print("Successive overlapping pairs of characters in a string- ", list(pairwise(string)))
pairwise
function from the itertools
module.lst
.lst
.pairwise
function with lst
as the argument.string
.pairwise
function with string
as the argument.Note: A string is iterable in python.