How to Compare Adjacent Elements in a List in Python
Last Updated :
14 Jan, 2025
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)
Output1 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):
Thi
s
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)
Output1 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])
Output1 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)
Output1 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.
Similar Reads
Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the
3 min read
Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the
3 min read
Python | Consecutive elements grouping in list Sometimes, while working with Python lists, we can have a problem in which we might need to group the list elements on basis of their respective consecutiveness. This kind of problem can occur while data handling. Let's discuss certain way in which this task can be performed. Method 1: Using enumera
5 min read
Check if two lists are identical in Python Our task is to check if two lists are identical or not. By "identical", we mean that the lists contain the same elements in the same order. The simplest way to check if two lists are identical using the equality operator (==).Using Equality Operator (==)The easiest way to check if two lists are iden
2 min read
Check if two lists are identical in Python Our task is to check if two lists are identical or not. By "identical", we mean that the lists contain the same elements in the same order. The simplest way to check if two lists are identical using the equality operator (==).Using Equality Operator (==)The easiest way to check if two lists are iden
2 min read
Python | Consecutive remaining elements in list Sometimes, while working with Python list, we can have a problem in which we need to get the consecutive elements count remaining( including current ), to make certain decisions beforehand. This can be a potential subproblem of many competitive programming competitions. Let's discuss a shorthand whi
2 min read