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.

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

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

ExampleEdit & Run
#import the pairwise function
from itertools import pairwise

L = [1, 2, 3, 4]

paired = list(pairwise(L))

print(list(paired))
Output:
[(1, 2), (2, 3), (3, 4)][Finished in 0.011185764102265239s]

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.

ExampleEdit & Run
from itertools import pairwise

data = 'ABCDEF'

for i in pairwise(data):
    print(i)
Output:
('A', 'B')('B', 'C')('C', 'D')('D', 'E')('E', 'F')[Finished in 0.010896865976974368s]