How to Remove Adjacent Duplicates from a Python List – 5 Simple Methods
This article walks through a common Python data‑processing challenge—removing consecutive duplicate elements from a list—by presenting five distinct solutions, including itertools.groupby, list comprehensions, explicit loops, generator functions, and enumerate, each illustrated with code snippets and output screenshots.
Introduction
A user asked how to process a list in Python so that consecutive duplicate values are collapsed, expecting the result [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4] from the original data
origin_lst = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4].
Implementation
Method 1 (itertools.groupby)
import itertools
origin_lst = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]
final_lst = [x[0] for x in itertools.groupby(origin_lst)]
print(final_lst)Running this code produces the expected output.
Method 2 (list comprehension)
origin_lst = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]
res = [origin_lst[i] for i in range(len(origin_lst)) if i == 0 or origin_lst[i] != origin_lst[i - 1]]
print(res)The result matches the expectation.
Method 3 (explicit loop)
origin_lst = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]
result = [origin_lst[0]]
for i in range(1, len(origin_lst)):
if origin_lst[i] != origin_lst[i-1]:
result.append(origin_lst[i])
print(result)The output is correct.
Method 4 (generator)
origin_lst = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]
def del_adjacent(iterable):
prev = object()
for item in iterable:
if item != prev:
prev = item
yield item
result = list(del_adjacent(origin_lst))
print(result)The generator also yields the desired list.
Method 5 (enumerate)
origin_lst = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]
lst_final = []
for index, val in enumerate(origin_lst):
if val != origin_lst[index - 1]:
lst_final.append(val)
print(lst_final)This approach also produces the correct result.
Conclusion
The article presented multiple ways to eliminate adjacent duplicates in a Python list, demonstrating that there are many viable solutions to this common data‑processing problem.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
