Answers for "remove all consecutive duplicates from the string"

1

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
Posted by: Guest on February-23-2020
0

removing consecutive duplicates

#removing consecutive duplicates

x = [1,2,4,7,3,7,8,4,4,9]

previous_value = None
new_lst = []

for elem in x:
   if elem != previous_value:
       new_lst.append(elem)
       previous_value = elem

print(new_lst)
>>> [1, 2, 4, 7, 3, 7, 8, 4, 9]
Posted by: Guest on June-03-2021

Code answers related to "remove all consecutive duplicates from the string"

Python Answers by Framework

Browse Popular Code Answers by Language