Open In App

How to Compare Adjacent Elements in a List in Python

Last Updated : 14 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

To compare adjacent elements in a list, we iterate through the list and compare each element with the next one to check if consecutive elements are equal or meet specific conditions.

Using the itertools.pairwise()

This is an efficient way to iterate through adjacent pairs. It generates consecutive pairs (a, b) from the input iterable without creating additional intermediate lists, making it both memory-efficient and fast.

Python
li = [1, 2, 2, 3, 4, 4, 5]

 # Loop through indices of the list
for i in range(len(li) - 1):
  
    a = li[i]  # current element
    b = li[i + 1] # next element
    
    print(a, b, " ", a == b)

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

Explanation:

  • range(len(li) - 1) : This generates indices from 0 to len(li) - 2 and ensures the loop stops at the second-to-last element.
  • print(a, b, " ", a == b):This prints the current element a , next element b, a space and whether a is equal to b.

Using zip()

zip() function in Python is a efficient tool for pairing elements from two or more iterables, such as lists. By combining the original list with a sliced version of itself . It pairs adjacent elements together, making it an ideal choice for tasks like comparing adjacent elements in a list.

Python
li = [1, 2, 2, 3, 4, 4, 5]

for a, b in zip(li, li[1:]):
    print(a, b, " ", a == b)

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

Explanation: zip(li, li[1:]) pairs each element of the list li with the next element by combining the original list li and its sliced version li[1:] .

Using List Comprehension

By using a single line of code, list comprehension generates a new list that holds tuples, where each tuple consists of the current element, the next element and the result of their comparison.

Python
li = [1, 2, 2, 3, 4, 4, 5]

# compare adjacent elements
res = [(li[i], li[i + 1], li[i] == li[i + 1]) for i in range(len(li) - 1)]

# Print result
for r in res:
    print(r[0], r[1], " ", r[2])

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

Explanation:

  • list comprehension: This iterates through the list li, pairing each element li[i] with the next element li[i + 1] and compares whether they are equal, storing the result as True or False in a tuple.
  • for r in res: This iterates through each tuple in the list and prints the elements alongside the comparison result .

Using loops

Using loops , we can manually iterate through a list and compare adjacent elements by accessing each element and its next neighbor. This approach requires explicit control over the loop indices, but it can become inefficient for larger lists due to redundant operations.

Python
li = [1, 2, 2, 3, 4, 4, 5]

# compare adjacent elements
for i in range(len(li) - 1):
    a = li[i]
    b = li[i + 1]
    print(a, b, " ", a == b)

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

Explanation: for i in range(len(li) - 1) loop iterates over the indices of the list li, stopping at the second-to-last element (len(li) - 1). This ensures that each element is compared with the next one without going out of bounds.


Next Article
Article Tags :
Practice Tags :

Similar Reads