The itertools module in the standard library provides a number of functions for conveniently iterating through iterables.

The pairwise() function in the module creates an iterator of tuples where each tuple contains overlapping consecutive elements from the input iterable.

pairwise(iterable)
iterable Required. The iterable containing the elements to be paired

The function returns an iterator which yields overlapping pairs taken from the input iterable during each consecutive call.

#import the pairwise function
from itertools import pairwise

L = [1, 2, 3, 4]

paired = list(pairwise(L))

print(list(paired))

In the above example, we started by importing the pairwise() function from itertools module.

We have a list L defined with values 1, 2, 3, 4. We use the pairwise() function to iterate over the list and pair each element with its adjacent element, and store the result in an object called paired.

We then print out the object to see our results.

from itertools import pairwise

data = 'ABCDEF'

for i in pairwise(data):
    print(i)