SoFunction
Updated on 2025-04-11

Several ways to compare adjacent elements in a list in Python

Compare each consecutive pair in a list in Python

Python Lists provides us with several sets of methods and libraries that can help us compare adjacent elements. To compare adjacent elements in a list, the list is usually traversed when accessing consecutive pairs of elements. This can be done using loops or list parsing.

For example:

Input: [1, 2, 2, 3, 4, 4, 5]
Output : 1 2   False
         2 2   True      
         2 3   False     
         3 4   False      
         4 4   True      
         4 5   False

Now let's take a look at several different approaches together to better understand the code examples.

1. Use For Loop

In this method, we will use a simple for loop to iterate through each element in the list. We will use the index to compare the element at the i-th position with the element at the (i+1)th position.

# function for comparision
def compare(my_list):
    for i in range(len(my_list)-1):
        # comparision between adjacant elements
        print(my_list[i], my_list[i+1], " ", my_list[i] == my_list[i+1])
    
# number list
compare([1, 2, 2, 3, 4, 4, 5])

Output

1 2   False
2 2   True
2 3   False
3 4   False
4 4   True
4 5   False

2. Use list analysis

In this method we will do the same method, but this time we will use list parsing techniques.

# function for comparision
def compare(my_list):
    # comparision between adjuscant elements
    newList = [[my_list[i], my_list[i+1], my_list[i] == my_list[i+1]] 
               for i in range(len(my_list)-1)]
    for i in newList:
        print(i[0], i[1], " ", i[2])
    
# string list
compare(['XFG','xfg','Coding','Apple','Python','Python'])

Output

XFG xfg   False
xfg Coding   False
Coding Apple   False
Apple Python   False
Python Python   True

3. Use the itertools function

itertools is a standard Python library that provides us with many ways to create and use iterators. We will use a pairwise function from the itertools library function. Let's look at the code to better understand how the function works.

# importing the pariwise function
from itertools import pairwise

def compare(my_list):
    #getting all the pairs and iterating over them
    for i, j in pairwise(my_list):
        #displaying the result
        print (i, j, " ", i==j)

#  number list
compare([1, 2, 2, 3, 4, 4, 5])

Output

1 2   False
2 2   True
2 3   False
3 4   False
4 4   True
4 5   False

4. Use the zip method

The zip() method is used to combine multiple iterators (such as lists, collections, dictionaries, etc.) into an iterator that combines multiple iterators (such as lists, collections, dictionaries, etc.) into one tuple. In this method, we will use the zip function and create a tuple of the i-th element and (i+1)th element of the given list. Let's look at the code implementation for a better understanding.

def compare(my_list):
    #getting all the pairs and iterating over them
    for i, j in zip(my_list, my_list[1:]):
        #displaying the result
        print (i,j," ",i==j)

# number list
compare([1, 2, 2, 3, 4, 4, 5])

Output

1 2   False
2 2   True
2 3   False
3 4   False
4 4   True
4 5   False

This is the end of this article about several methods to compare adjacent elements in lists in Python. For more related contents of adjacent elements in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!