In this tutorial, we will look at how to remove consecutive duplicates from a string in Python with the help of some examples.
How to remove consecutive duplicates from a string?
You can use a loop to iterate over each character in a string and create a new string with the consecutive duplicates removed. The following are the steps –
- Iterate over each character in the string.
- For each character check if it’s the same as the previous character (stored in a variable). If it is, skip to the next iteration, else add the character to our result string.
- Return the result string.
Note that, strings are immutable in Python, thus, you cannot modify the original string but you can create a new string with the consecutive duplicates removed.
You can use the above method in programming languages other than Python (for example, C++, Java, etc.) since it does not use any language-specific functions or constructs.
Let’s write a function in Python to perform the above steps.
# remove consecutive duplicates from string def remove_consec_duplicates(s): new_s = "" prev = "" for c in s: if len(new_s) == 0: new_s += c prev = c if c == prev: continue else: new_s += c prev = c return new_s
Now, let’s apply the above function on the string “aabbbbcccd”.
# string with consecutive duplicates s = "aabbbbcccd" print(s) # remove consecutive duplicates s = remove_consec_duplicates(s) print(s)
Output:
aabbbbcccd abcd
Here we remove consecutive duplicates from string s and store the resulting string back to s. Note that the resulting string does not have any adjacent repeating characters.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Note that this function does not give a string of unique characters from the string rather it removes adjacent repeating characters. For example –
# string with consecutive duplicates s = "aabbcccaaaa" print(s) # remove consecutive duplicates s = remove_consec_duplicates(s) print(s)
Output:
aabbcccaaaa abca
In the above result, you can see that we get 'a'
two times in the resulting string, this is because our objective is to remove consecutive duplicates and not to get unique characters in the string.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.