Python – Squash consecutive values of K

Sometimes, while working with Python lists, we can have a problem in which we need to perform removal of duplicate consecutive values of particular element. This kind of problem can have application in many domains such as competitive programming and web development. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [4, 5, 5, 4, 3, 3, 5, 5, 5, 6, 5], K = 3
Output : [4, 5, 5, 4, 3, 5, 5, 5, 6, 5]

Input : test_list = [1, 1, 1, 1], K = 1
Output : [1]

Method #1 : Using zip() + yield
The combination of above functions provide one of the ways to solve this problem. In this, we perform the task of checking consecution duplications using zipping current and next element list, and yield is used to render the element basis of condition.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Squash consecutive values of K
# Using zip() + yield
  
# helper function
def hlper_fnc(test_list, K):
    for sub1, sub2 in zip(test_list, test_list[1:]):
        if sub1 == sub2 == K:
            continue
        else:
            yield sub1
    yield sub2       
  
# initializing list
test_list = [4, 5, 5, 4, 3, 3, 5, 5, 5, 6, 5
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 5
  
# Squash consecutive values of K
# Using zip() + yield
res = list(hlper_fnc(test_list, K))
  
# printing result 
print("List after filteration : " + str(res))

chevron_right


Output :



The original list is : [4, 5, 5, 4, 3, 3, 5, 5, 5, 6, 5]
List after filteration : [4, 5, 4, 3, 3, 5, 6, 5]

 

Method #2 : Using yield + groupby()
The combination of above functions can be used to solve this problem. In this, we perform the task of grouping using groupby(), for checking consecutions, and yield is used to return valid elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Squash consecutive values of K
# Using yield + groupby()
import itertools
  
# helper function
def hlper_fnc(test_list, K):
    for key, val in itertools.groupby(test_list):
        if key == K:
            yield key
        else:
            yield from val    
  
# initializing list
test_list = [4, 5, 5, 4, 3, 3, 5, 5, 5, 6, 5
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 5
  
# Squash consecutive values of K
# Using yield + groupby()
res = list(hlper_fnc(test_list, K))
  
# printing result 
print("List after filteration : " + str(res))

chevron_right


Output :

The original list is : [4, 5, 5, 4, 3, 3, 5, 5, 5, 6, 5]
List after filteration : [4, 5, 4, 3, 3, 5, 6, 5]



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.