Open In App

Python – Squash consecutive values of K

Last Updated : 14 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python lists, we can have a problem in which we need to perform the removal of duplicate consecutive values of a particular element. This kind of problem can have applications 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 the above functions provides 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.
 

Python3




# 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 filtration : " + str(res))


Output : 

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

 

The time complexity of the program is O(n) because the helper function iterates through the input list once using the zip() function, which has a time complexity of O(n). Additionally, the yield statement in the helper function does not cause any extra iterations over the input list. The final list() function also has a time complexity of O(n) because it converts the generator object returned by the hlper_fnc() function into a list.

The auxiliary space complexity of the program is O(n) because the program creates a new list with filtered elements that can be as large as the original list. 

 
Method #2 : Using yield + groupby() 
The combination of the 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.
 

Python3




# 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 filtration : " + str(res))


Output : 

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

 

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  yield + groupby() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list



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

Similar Reads