Open In App

Python | Consecutive duplicates all elements deletion in list

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 applications as well. Let’s discuss a shorthand in which this task can be performed. 

Method 1: Using list comprehension + groupby() + sum() 

This task can be performed using a combination of the above functions. The first step is grouping the elements into duplicates using groupby(), and then if they occur more than 0 times, remove them, i.e include only those elements that occur once. This counting task is handled by sum() method.

Example: 

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, 1, 3, 4, 4, 4, 5, 6, 6, 7, 8, 8, 6]
List after consecutive duplicates elements deletion : [3, 5, 7, 6]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new res list 

Method 2: Using for loop

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, 1, 3, 4, 4, 4, 5, 6, 6, 7, 8, 8, 6]
List after consecutive duplicates elements deletion : [3, 5, 7, 6]

Time complexity: O(n) as it requires iterating over the entire list once.
Auxiliary space: O(n) as it requires creating a new list to store the non-consecutive duplicates.

METHOD 3:Using re method

APPROACH:

The program uses the Python re module to remove consecutive duplicate elements from a list. The input list is first converted to a string with space-separated elements using join and map. Then, re.sub is used to remove any consecutive duplicates using a regular expression pattern. Finally, the resulting string is converted back to a list of integers using split and map.

ALGORITHM:

1. Convert the input list to a string with space-separated elements using join and map.
2. Use re.sub to remove any consecutive duplicates using a regular expression pattern that matches any digit (\d+) followed by one or more occurrences of the same digit (using a backreference to the first capture group with \1), surrounded by word boundaries (\b).
3. Convert the resulting string back to a list of integers using split and map.
4. Return the resulting list.

Python3




import re
 
original_list = [1, 1, 3, 4, 4, 4, 5, 6, 6, 7, 8, 8, 6]
 
temp_str = ' '.join(map(str, original_list))
temp_str = re.sub(r'\b(\d+)(?: \1\b)+', r'', temp_str)
temp_list = list(map(int, temp_str.split()))
 
print("Original List: ", original_list)
print("List after consecutive duplicates deletion: ", temp_list)


Output

Original List:  [1, 1, 3, 4, 4, 4, 5, 6, 6, 7, 8, 8, 6]
List after consecutive duplicates deletion:  [3, 5, 7, 6]

Time complexity:
The time complexity of the program is O(n), where n is the number of elements in the input list. The join and split functions each have a time complexity of O(n), while re.sub has a time complexity of O(n) for simple regular expressions like the one used here.

Space complexity:
The space complexity of the program is O(n), where n is the number of elements in the input list. The program creates a temporary string of length O(n) using join and map, and a temporary list of length O(n) using split and map. In addition, the re module may use additional space to store intermediate results during regular expression processing.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads