Last Updated : 10 Jan, 2025
In Python, the groupby
method from the itertools
module provides a powerful way to group consecutive identical elements in an iterable. This functionality can be effectively utilized to remove all consecutive duplicates from a sequence. By retaining only the first occurrence of each group of identical elements, we can clean up a list or string while preserving its overall structure.
Let’s start with a simple example to see how the groupby
method can help us remove consecutive duplicates:
from itertools import groupby
# Example list with consecutive duplicates
s = [1, 1, 2, 3, 3, 4]
# Removing consecutive duplicates using groupby
res = [key for key, _ in groupby(s)]
print(res)
Explanation:
groupby
function groups consecutive identical elements in the sequence
.result
list.
itertools.groupby(iterable, key=None)
Parameters
iterable
: The input iterable to be grouped.key
(optional): A function to specify a criterion for grouping. By default, consecutive identical elements are grouped.Return Type
Before diving into the implementation, consider a list with repetitive elements:
Python
from itertools import groupby
# List with consecutive duplicates
s = ["a", "a", "b", "b", "c"]
# Remove consecutive duplicates
res = [key for key, _ in groupby(s)]
print(res)
Explanation:
groupby
method groups consecutive identical elements.Key
variable represents the unique elements in each group, forming the final cleaned list.Let’s apply the groupby
method to a string:
from itertools import groupby
# String with consecutive duplicates
s = "aabbcc"
# Remove consecutive duplicates
res = "".join(key for key, _ in groupby(s))
print(res)
Explanation:
"".join()
, the unique keys are concatenated into a single string without consecutive duplicates.Strings can be processed in a case-sensitive manner by default. Here’s how it works:
Python
from itertools import groupby
# String with mixed-case duplicates
s = "aaAAaabb"
# Remove consecutive duplicates
res = "".join(key for key, _ in groupby(s))
print(res)
Explanation:
Key
parameter can be used for advanced grouping scenarios. For instance, treating uppercase and lowercase letters as identical:
from itertools import groupby
# String with mixed-case duplicates
s = "aaAAaabb"
# Remove consecutive duplicates ignoring case
res = "".join(key for key, _ in groupby(s, key=str.lower))
print(res)
Explanation:
str.lower
function is passed as the key
, ensuring that grouping ignores case differences.groupby
method be used with data types other than lists and strings?
2. How does theYes, the
groupby
method works with any iterable, including tuples and generators:from itertools import groupby
# Tuple with consecutive duplicates
a = (1, 1, 2, 2, 3)
res = [key for key, _ in groupby(a)]
print(res) # Output: [1, 2, 3]
groupby
method handle non-consecutive duplicates?
3. Can we useThe
groupby
method only groups consecutive identical elements. Non-consecutive duplicates remain in the result:from itertools import groupby
# List with non-consecutive duplicates
s = [1, 2, 1, 2]
res = [key for key, _ in groupby(s)]
print(res) # Output: [1, 2, 1, 2]
groupby
for numeric processing?
Absolutely. The method works seamlessly with numeric data:
from itertools import groupby
# Numeric data
n = [1, 1, 2, 2, 3, 3]
res = [key for key, _ in groupby(n)]
print(res) # Output: [1, 2, 3]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4