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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]

 
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.