Open In App

# 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 comprehension Using the groupby function, we can group the together occurring elements as one and can remove all the duplicates in succession and just let one element be in the list.

## Python3

 `# Python3 code to demonstrate``# removing consecutive duplicates``# using groupby() + list comprehension``from` `itertools ``import` `groupby` `# initializing list``test_list ``=` `[``1``, ``4``, ``4``, ``4``, ``5``, ``6``, ``7``, ``4``, ``3``, ``3``, ``9``]` `# printing original list``print` `("The original ``list` `is` `: " ``+` `str``(test_list))` `# using groupby() + list comprehension``# removing consecutive duplicates``res ``=` `[i[``0``] ``for` `i ``in` `groupby(test_list)]` `# printing result``print` `("The ``list` `after removing consecutive duplicates : " ``+`  `str``(res))`

Output:

```The original list is : [1, 4, 4, 4, 5, 6, 7, 4, 3, 3, 9]
The list after removing consecutive duplicates : [1, 4, 5, 6, 7, 4, 3, 9]```

Time complexity: O(n), where n is the number of elements in the list. This is because the groupby function iterates through the list once, making it an O(n) operation.
Auxiliary space: O(m), where m is the number of unique elements in the list. This is because the resulting list only contains unique elements and its size is equal to the number of unique elements.

Method #2 : Using zip_longest() + list comprehension This function can be used to keep the element and delete the successive elements with the use of slicing. The zip_longest function does the task of getting the values together in the one list.

## Python3

 `# Python3 code to demonstrate``# removing consecutive duplicates``# using zip_longest()+ list comprehension``from` `itertools ``import` `zip_longest` `# initializing list``test_list ``=` `[``1``, ``4``, ``4``, ``4``, ``5``, ``6``, ``7``, ``4``, ``3``, ``3``, ``9``]` `# printing original list``print` `("The original ``list` `is` `: " ``+` `str``(test_list))` `# using zip_longest()+ list comprehension``# removing consecutive duplicates``res ``=` `[i ``for` `i, j ``in` `zip_longest(test_list, test_list[``1``:])``                                                ``if` `i !``=` `j]` `# printing result``print` `("``List` `after removing consecutive duplicates : " ``+`  `str``(res))`

Output:

```The original list is : [1, 4, 4, 4, 5, 6, 7, 4, 3, 3, 9]
List after removing consecutive duplicates : [1, 4, 5, 6, 7, 4, 3, 9]```

Time complexity: O(n), where n is the length of the input list “test_list”.
Auxiliary space complexity: O(1), as only a few variables are used in the code and no extra data structures are being created.

Method #3 : Using iteration Approach is using a for loop to iterate through the list and a temporary variable to store the last seen element. You can then check if the current element is the same as the last seen element, and if it is, you can skip it and continue to the next iteration. If it is not a duplicate, you can append it to a new list and update the temporary variable to the current element. This approach is simple and easy to understand, but it may not be as efficient as using itertools functions like groupby or zip_longest.

Here is an example of how this approach can be implemented:

## Python3

 `def` `remove_consecutive_duplicates(lst):``    ``# temporary variable to store the last seen element``    ``last_seen ``=` `None``    ``# list to store the elements after removing consecutive duplicates``    ``res ``=` `[]``    ``for` `x ``in` `lst:``        ``# if x is not a duplicate of the last seen element, append it to the result list``        ``if` `x !``=` `last_seen:``            ``res.append(x)``        ``# update the last seen element``        ``last_seen ``=` `x``    ``return` `res``#This code is contributed by Edula Vinay Kumar Reddy``# test the function``test_list ``=` `[``1``, ``4``, ``4``, ``4``, ``5``, ``6``, ``7``, ``4``, ``3``, ``3``, ``9``]``print``(remove_consecutive_duplicates(test_list)) ``# [1, 4, 5, 6, 7, 4, 3, 9]`

Output

`[1, 4, 5, 6, 7, 4, 3, 9]`

Time complexity: O(n), where n is the length of the input list. This is because the for loop iterates through the list once, and the rest of the operations (checking if the current element is the same as the last seen element and appending the element to the result list) are constant time operations.
Auxiliary space: O(n), because it creates a new list to store the elements after removing consecutive duplicates, and the size of this list is equal to the number of elements in the input list. This means that the space complexity is directly proportional to the size of the input.