How to Remove Adjacent Duplicates in a Python List – 5 Simple Methods
This article walks through a Python list‑deduplication problem, showing the original data, the expected result, and five distinct code solutions—including itertools, list comprehensions, loops, generators, and enumerate—each illustrated with output screenshots.
Introduction
Hi everyone, I’m Pipí. In a recent Python group chat a member asked how to process a list to remove adjacent duplicate values. The original list is
origin_lst = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]and the desired output is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4].
Method 1 – itertools.groupby
Using itertools.groupby to collapse consecutive duplicates:
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)The result matches the expectation.
Method 2 – List Comprehension
A list‑comprehension that checks each element against its predecessor:
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)Method 3 – Simple Loop
A straightforward loop that appends a value only when it differs from the previous one:
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)Method 4 – Generator Function
A generator that yields values only when they differ from the previously yielded item:
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)Method 5 – enumerate
Using enumerate to compare each element with the previous one by index:
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)Conclusion
The article presented a Python data‑processing challenge and offered multiple clear solutions, helping the community solve the problem efficiently. Thanks to the contributors who shared their ideas and code.
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.
