return a list of items without any elements with the same value next to each other

Use existing libraries to perform that task, like itertools.groupby

import itertools

def unique_in_order(iterable):
    return [k for k,_ in itertools.groupby(iterable)]

print(unique_in_order('AAAABBBCCDAABBB')) # ['A', 'B', 'C', 'D', 'A', 'B']
print(unique_in_order(['A']))  # ['A']

With the default group key, groupby groups identical consecutive elements, yielding tuples with the value and the group of values (that we ignore here, we just need the key)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top