How to Remove Adjacent Duplicates in a Python List: 5 Simple Methods
Learn five practical Python techniques—including itertools.groupby, list comprehensions, loops, generators, and enumerate—to efficiently eliminate consecutive duplicate elements from a list, with clear code examples and visual results that help you understand and apply each method.
1. Introduction
Hello everyone, I am Pipi. A few days ago a question was asked in the Python community group "Chloe" about processing a list to remove adjacent duplicate values. The original data is:
origin_lst = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]The expected result is:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4]2. Implementation
Method 1
Using 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)Method 2
Using a list comprehension with index checking:
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
Using a simple 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)Method 4
Using a generator function:
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
Using 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)3. Summary
This article presented a Python data‑processing problem and offered multiple concrete solutions—using itertools.groupby, list comprehensions, simple loops, a generator, and enumerate —to help fans successfully remove adjacent duplicates from a list.
Thanks to the contributors who provided the ideas and code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
