Home >Backend Development >Python Tutorial >How Can I Efficiently Remove Consecutive Duplicate Elements from a Python List?

How Can I Efficiently Remove Consecutive Duplicate Elements from a Python List?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-01 09:37:09584browse

How Can I Efficiently Remove Consecutive Duplicate Elements from a Python List?

Removing Elements with Consecutive Duplicates

A common task in data processing is eliminating consecutive duplicates from a list. A straightforward Python implementation might be:

list = [1,1,1,1,1,1,2,3,4,4,5,1,2]
i = 0

while i < len(list)-1:
    if list[i] == list[i+1]:
        del list[i]
    else:
        i = i+1

This approach removes repeated elements, resulting in an output like [1, 2, 3, 4, 5, 1, 2].

Eliminating Elements Whose Values Repeat Consecutively

However, an improved goal is to remove entire elements whose values repeat consecutively, resulting in an output like [2, 3, 5, 1, 2]. The previous approach can be modified:

list = [1,1,1,1,1,1,2,3,4,4,5,1,2]
i = 0
dupe = False

while i < len(list)-1:
    if list[i] == list[i+1]:
        del list[i]
        dupe = True
    elif dupe:
        del list[i]
        dupe = False
    else:
        i += 1

While functional, this approach could be simplified.

A More Elegant Solution

Python provides more expressive tools for list manipulation. Using itertools.groupby:

L = [1,1,1,1,1,1,2,3,4,4,5,1,2]
from itertools import groupby
[key for key, _group in groupby(L)]

This yields the desired output: [1, 2, 3, 4, 5, 1, 2].

For the second part of the task:

[k for k, g in groupby(L) if len(list(g)) < 2]

This uses groupby to group consecutive duplicates and filters out groups with more than one element, effectively removing the duplicated elements.

If desired, you can avoid creating a temporary list by using a generator expression:

[k for k, g in groupby(L) if sum(1 for i in g) < 2]

The above is the detailed content of How Can I Efficiently Remove Consecutive Duplicate Elements from a Python List?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn