Horje
How to Compare Adjacent Elements in a List in Python

In Python, we have several ways to compare adjacent elements in a list. We do not see any direct or indirect applications for comparing adjacent elements, such as identifying recent trends, optimizing user experience, stock market analysis, and many more. In this article, we are going to compare adjacent Elements in a list in Python.

Comparing Each Consecutive Pair in a List in Python

Python Lists provides us with several sets of methods and libraries that will help us compare adjacent elements. To compare adjacent elements in a list, you typically iterate through the list while accessing pairs of consecutive elements. This can be done using loops or comprehensions.

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 us see a few different methods along with the code example for a better understanding.

Using For Loop

In this method, we will loop through each element of the list using a simple for loop. We will use indexing to compare elements at ith position to the element at (i+1)th position.

Python
# 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

Using List Comprehension

In this method, we are going to preform the same method but this time we will use List Comprehension technique.

Python
# 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(['GFG','gfg','Coding','Apple','Python','Python'])

Output:

GFG gfg   False
gfg Coding False
Coding Apple False
Apple Python False
Python Python True

Using itertools function

The itertools is a standard Python library which provides us number of methods to create and use an iterator. We will be using one of the itertools library function which is pairwise function. Lets see the code for better understanding of the working of the function.

Python
# 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

Using zip() function

The zip() function is used to combine multiple iterables(such as list, sets, dictionary etc) into a single iterator of tuple. In this method, we will use zip function and create a tuple of ith element and (i+1)th element of the given list. Lets see the code implementation for better understanding.

Python
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



Reffered: https://www.geeksforgeeks.org


Python

Related
How to Plot Multiple DataFrames in Subplots in Python How to Plot Multiple DataFrames in Subplots in Python
Integrate the QuickBooks API with the Python Django Integrate the QuickBooks API with the Python Django
SQLAlchemy Tutorial SQLAlchemy Tutorial
How to Clone and Save a Django Model Instance to the Database How to Clone and Save a Django Model Instance to the Database
Microservice in Python using FastAPI Microservice in Python using FastAPI

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19