Open In App

Python groupby method to remove all consecutive duplicates

Last Updated : 28 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S, remove all the consecutive duplicates. Examples:

Input  : aaaaabbbbbb
Output : ab

Input : geeksforgeeks
Output : geksforgeks

Input : aabccba
Output : abcba

We have existing solution for this problem please refer Remove all consecutive duplicates from the string link. We can solve this problem in python quickly using itertools.groupby() method.

How itertools.groupby(iterable,key[optional]) works in Python?

Group by method takes two input one is iterable (list,tuple,dictionary) and second is key function which calculates keys for each element present in iterable. It returns key and iterable of grouped items. If key function not specified or is None, key defaults to an identity function and returns the element unchanged. For example, 

Python3




numbers = [1, 1, 1, 3, 3, 2, 2, 2, 1, 1]
import itertools
for (key,group) in itertools.groupby(numbers):
    print (key,list(group))


Output

1 [1, 1, 1]
3 [3, 3]
2 [2, 2, 2]
1 [1, 1]

Python3




# function to remove all consecutive duplicates
# from the string in Python
 
from itertools import groupby
def removeAllConsecutive(input):
     
    # group all consecutive characters based on their
    # order in string and we are only concerned
    # about first character of each consecutive substring
    # in given string, so key value will work for us
    # and we will join these keys without space to
    # generate resultant string
    result = []
    for (key,group) in groupby(input):
        result.append(key)
 
    print (''.join(result))
     
# Driver program
if __name__ == "__main__":
    input = 'aaaaabbbbbb'
    removeAllConsecutive(input)


Output

ab

Time complexity : O(n) 
Auxiliary Space : O(n)



Similar Reads

Remove all consecutive duplicates from the string
Given a string S, The task is to remove all the consecutive duplicate characters of the string and return the resultant string. Note: that this problem is different from Recursively remove all adjacent duplicates. Here we keep one character and remove all subsequent same characters. Examples: Input: S= "aaaaabbbbbb"Output: ab Input: S = "geeksforge
14 min read
Python | Remove consecutive duplicates from list
In Python, we generally wish to remove the duplicate elements, but sometimes for several specific usecases, we require to have remove just the elements repeated in succession. This is a quite easy task and having a shorthand for it can be useful. Let's discuss certain ways in which this task can be performed. Method #1 : Using groupby() + list comp
4 min read
Python - Remove alternate consecutive duplicates
Given list of elements, remove alternate consecutive duplicates of elements. Input : test_list = [5, 5, 5, 5, 6, 6] Output : [5, 5, 6] Explanation : Alternate occ. of 5 and 6 are removed. Input : test_list = [5, 5, 5, 5] Output : [5, 5] Explanation : Alternate occ. of 5 are removed. Method : Using loop + remove() The combination of above functions
2 min read
Remove three consecutive duplicates from string
Given a string, you have to remove the three consecutive duplicates from the string. If no three are consecutive then output the string as it is. Examples: Input : aabbbaccddddcOutput :ccdcInput :aabbaccddcOutput :aabbaccddcRecommended PracticeThree consecutive duplicatesTry It!Explanation : We insert the characters of string one by one to vector a
12 min read
Python | Consecutive duplicates all elements deletion in list
Sometimes, while working with Python list, a problem can occur to filter list to remove duplicates. The solution to this has been discussed before. But sometimes, we may have a problem in which we need to delete the duplicate and element itself if it occurs more than 1 in consecution. This type of problem can occur in day-day programming and other
4 min read
Check if string is palindrome after removing all consecutive duplicates
Given a string str, the task is to remove all the consecutive duplicates from the string str and check if the final string is palindrome or not. Print "Yes" if it is a palindromic else print "No". Examples: Input: str = "abbcbbbaaa" Output: Yes Explanation: On removing all consecutive duplicates characters, the string becomes "abcba" which is a pal
7 min read
Pandas dataframe.groupby() Method
Pandas groupby is used for grouping the data according to the categories and applying a function to the categories. It also helps to aggregate data efficiently. The Pandas groupby() is a very powerful function with a lot of variations. It makes the task of splitting the Dataframe over some criteria really easy and efficient. Pandas dataframe.groupb
7 min read
Python | Remove all duplicates words from a given sentence
Given a sentence containing n words/strings. Remove all duplicates words/strings which are similar to each others. Examples: Input : Geeks for Geeks Output : Geeks for Input : Python is great and Java is also great Output : is also Java Python and great We can solve this problem quickly using python Counter() method. Approach is very simple. 1) Spl
7 min read
Python | Remove all duplicates and permutations in nested list
Given a nested list, the task is to remove all duplicates and permutations in that nested list. Input: [[-11, 0, 11], [-11, 11, 0], [-11, 0, 11], [-11, 2, -11], [-11, 2, -11], [-11, -11, 2]] Output: {(-11, 0, 11), (-11, -11, 2)} Input: [[-1, 5, 3], [3, 5, 0], [-1, 5, 3], [1, 3, 5], [-1, 3, 5], [5, -1, 3]] Output: {(1, 3, 5), (0, 3, 5), (-1, 3, 5)}
4 min read
Python Program To Recursively Remove All Adjacent Duplicates
Given a string, recursively remove adjacent duplicate characters from the string. The output string should not have any adjacent duplicates. See the following examples. Examples: Input: azxxzy Output: ay First "azxxzy" is reduced to "azzy". The string "azzy" contains duplicates, so it is further reduced to "ay". Input: geeksforgeeg Output: gksfor F
4 min read
Article Tags :
Practice Tags :