Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

remove consecutive duplicates python

def remove_consecutive_duplicates(_list: list):
    preprocessed_list = []
    for x in itertools.groupby(_list):
        preprocessed_list.append(x[0])

    return preprocessed_list
Comment

python remove consecutive duplicates

no_consecutive_duplicates = [x[0] for x in itertools.groupby(my_list)]
Comment

Eliminate consecutive duplicates from a string in python

# Remove the Consecutive Duplicates string word:
# First System:
'''
s = input() #input: BBBBBPPIIAICCODEE
s2 = ""
prev = None
for chr in s:
    if prev != chr:
        s2 += chr
        prev = chr
print(s2)

output: BPIAICODE

'''

# Second System:

# input: SSSWWWIIFTPPYTHOOOOOOOOONNNNNNNNN
def removeConsecutiveDuplicates(s):
    if len(s) < 2:
        return s
    if s[0] != s[1]:
        return s[0] + removeConsecutiveDuplicates(s[1:])
    return removeConsecutiveDuplicates(s[1:])

# This code is contributed:
s1 = input()
print(removeConsecutiveDuplicates(s1))
# output: SWIFTPYTHON
Comment

Remove all consecutive duplicates from the string in python

# Remove the Consecutive Duplicates string word:
# First System:

#Example input: BBBBBPPIIAICCODEE

s = input() 
s2 = ""
prev = None
for chr in s:
    if prev != chr:
        s2 += chr
        prev = chr
print(s2)

output: BPIAICODE



# Second System:

# Example input: SSSWWWIIFTPPYTHOOOOOOOOONNNNNNNNN
'''
def removeConsecutiveDuplicates(s):
    if len(s) < 2:
        return s
    if s[0] != s[1]:
        return s[0] + removeConsecutiveDuplicates(s[1:])
    return removeConsecutiveDuplicates(s[1:])

# This code is contributed:
s1 = input()
print(removeConsecutiveDuplicates(s1))
# output: SWIFTPYTHON
'''
Comment

PREVIOUS NEXT
Code Example
Python :: python write list to text file 
Python :: datetime to int python 
Python :: python for loop m to n 
Python :: hide particular attribute in django admin 
Python :: how to make nmap port scanner in python 
Python :: dict godot 
Python :: likeliness python 
Python :: Goal Parser Python 
Python :: how to clear a text file in python 
Python :: get most recent file in directory python 
Python :: python check if all dictionary values are False 
Python :: switch columns and rows python 
Python :: display current local time in readable format 
Python :: turn of warning iin python 
Python :: pathlib recursive search 
Python :: how to change web browser in python 
Python :: convert bytes to numpy array python 
Python :: python zip file open as text 
Python :: django.db.utils.OperationalError: no such table: 
Python :: pandas sort values group by 
Python :: rename files in folder python 
Python :: python ndarray string array into int 
Python :: normalize = true pandas 
Python :: python count lines in string 
Python :: qtextedit get text 
Python :: python watchgod 
Python :: python how to copy a 2d array leaving out last column 
Python :: how to delete a turtle in python 
Python :: python know the number of a loop 
Python :: how to use enumerate instead of range and len 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =