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 can be used to solve this problem. In this, we iterate each element and use additional previous element variable to keep track of alternate criteria. The removal is performed using remove().

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Remove alternate consecutive duplicates
# Using loop + remove()
  
# initializing lists
test_list = [5, 5, 5, 5, 6, 6, 8, 3, 3, 8]
  
# printing original list
print("The original list : " + str(test_list))
  
# Using loop to iterate through elements
# element to keep track
temp = test_list[0]
count = 0
org_list = test_list
idx = 0
while(1):
      
    # break when idx greater than size
    if idx >= len(org_list):
        break
      
    # check for alternates 
    if count % 2 and temp == test_list[idx]:
        test_list.remove(test_list[idx])
        idx = idx - 1
        count += 1
        temp = test_list[idx]
    else:
          
        # keeping track of alternate index increment
        # and assignment
        if temp != test_list[idx]:
            count = 1
            temp = test_list[idx]
        else :
            count += 1
    idx = idx + 1
  
# printing result 
print("List after alternate duplicates removal : " + str(test_list))

chevron_right


Output

The original list : [5, 5, 5, 5, 6, 6, 8, 3, 3, 8]
List after alternate duplicates removal : [5, 5, 6, 8, 3, 8]



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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.