Python - Remove K length Duplicates from String Last Updated : 18 Jan, 2025 Comments Improve Suggest changes Like Article Like Report To remove consecutive K-length duplicates from a string iterate through the string comparing each substring with the next and excluding duplicates. For example we are given a string s = "abcabcabcabc" we need to remove k length duplicate from the string so that the output should become "aaaabc" . We can use methods like Counter from collection , string replacement, list comprehension .Using collections.CounterMethod counts substrings of length K and removes those that appear exactly K times. Python from collections import Counter s = "abcabcabcabc" k = 3 # Count the occurrences of each substring of length K sub_c = Counter(s[i:i+k] for i in range(len(s) - k + 1)) # Remove substrings that appear exactly K times res = ''.join([s[i] for i in range(len(s) - k + 1) if sub_c[s[i:i+k]] != k] + [s[i] for i in range(len(s)-k+1, len(s))]) print(res) Outputaaaabc Explanation:Counter() counts occurrences of all substrings of length K in the string s.New string is built by excluding substrings that appear exactly K times using a condition.Using String ReplacementMethod uses the replace() function to remove K-length duplicates explicitly. Python s = "abcabcabcabc" k = 3 # Loop to find and remove duplicates of length K for i in range(len(s) - k + 1): if s[i:i+k] == s[i+k:i+2*k]: # Check for consecutive K-length duplicates s = s[:i+k] + s[i+2*k:] print(s) Outputabcabc Explanation:Loop iterates through the string, checking if two consecutive substrings of length K are identical using s[i:i+K] == s[i+K:i+2*K].Consecutive duplicates are found string is updated by removing the second duplicate substring using slicing (s[:i+K] + s[i+2*K:])Using Set and Sliding WindowThis method uses a sliding window to keep track of substrings and removes duplicates by checking if the substring appears more than once. Python s = "abcabcabcabc" k = 3 res = [] seen = set() # Traverse the string with a sliding window of size K for i in range(len(s) - k + 1): sub = s[i:i+k] if sub not in seen: res.append(sub) seen.add(sub) print(''.join(res)) Outputabcbcacab Explanation:Loop iterates through the string, checking if two consecutive substrings of length K are identical using s[i:i+K] == s[i+K:i+2*K].Consecutive duplicates are found string is updated by removing the second duplicate substring using slicing (s[:i+K] + s[i+2*K:])Using List ComprehensionThis method checks for duplicate substrings of length K and removes them by comparing substrings in a list comprehension. Python s = "abcabcabcabc" k = 3 # Using list comprehension to remove K-length duplicates res = ''.join([s[i:i+k] for i in range(len(s) - k + 1) if s[i:i+k] not in s[i+k:i+2*k]]) print(res) Outputbcacababc Explanation:List comprehension iterates over the string, extracting substrings of length K and checks if each substring is not repeated in the next consecutive substring of the same length.Substrings that are not duplicates are joined together using ''.join(), forming the final string without consecutive duplicates of length K. Comment More infoAdvertise with us Next Article Python - Remove K length Duplicates from String manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Remove Duplicate Strings from a List in Python Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values.Pythona = ["Learn", "Python", "With" 3 min read Python | Duplicate substring removal from list Sometimes we can come to the problem in which we need to deal with certain strings in a list that are separated by some separator and we need to remove the duplicates in each of these kinds of strings. Simple shorthands to solve this kind of problem is always good to have. Let's discuss certain ways 7 min read Python - Remove duplicate words from Strings in List Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The 6 min read Python - Remove Duplicates from a List Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets 2 min read Python | Removing duplicates from tuple Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() 4 min read Python - Remove Duplicates from a list And Keep The Order While lists provide a convenient way to manage collections of data, duplicates within a list can sometimes pose challenges. In this article, we will explore different methods to remove duplicates from a Python list while preserving the original order.Using dict.fromkeys()dict.fromkeys() method creat 2 min read Python - Remove String from String List This particular article is indeed a very useful one for Machine Learning enthusiast as it solves a good problem for them. In Machine Learning we generally encounter this issue of getting a particular string in huge amount of data and handling that sometimes becomes a tedious task. Lets discuss certa 4 min read Python program to remove K length words in String Given a String, write a Python program to remove all the words with K length. Examples: Input : test_str = 'Gfg is best for all geeks', K = 3 Output : is best geeks Explanation : Gfg, for and all are of length 3, hence removed. Input : test_str = 'Gfg is best for all geeks', K = 2 Output : Gfg best 5 min read Python - Remove front K characters from each string in String List Sometimes, we come across an issue in which we require to delete the first K characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a 6 min read Python | Remove duplicates based on Kth element tuple list Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop Th 8 min read Like