Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove consecutive duplicates from a list? [duplicate]

How do I remove consecutive duplicates from a list like this in python?

lst = [1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]

Having a unique list or set wouldn't solve the problem as there are some repeated values like 1,...,1 in the previous list.

I want the result to be like this:

newlst = [1,2,4,1,3,5]

Would you also please consider the case when I have a list like this [4, 4, 4, 4, 2, 2, 3, 3, 3, 3, 3, 3] and I want the result to be [4,2,3,3] rather than [4,2,3] .

like image 636
Elmahy Avatar asked Aug 30 '16 21:08

Elmahy


3 Answers

itertools.groupby() is your solution.

newlst = [k for k, g in itertools.groupby(lst)]

If you wish to group and limit the group size by the item's value, meaning 8 4's will be [4,4], and 9 3's will be [3,3,3] here are 2 options that does it:

import itertools

def special_groupby(iterable):
    last_element = 0
    count = 0
    state = False
    def key_func(x):
        nonlocal last_element
        nonlocal count
        nonlocal state
        if last_element != x or x >= count:
            last_element = x
            count = 1
            state = not state
        else:
            count += 1
        return state
    return [next(g) for k, g in itertools.groupby(iterable, key=key_func)]

special_groupby(lst)

OR

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.zip_longest(*args, fillvalue=fillvalue)

newlst = list(itertools.chain.from_iterable(next(zip(*grouper(g, k))) for k, g in itertools.groupby(lst)))

Choose whichever you deem appropriate. Both methods are for numbers > 0.

like image 89
Bharel Avatar answered Oct 19 '22 01:10

Bharel


list1 = ['a', 'a', 'a', 'b', 'b' , 'a', 'f', 'c', 'a','a']
temp_list = []


for item in list1:   
   if len(temp_list) == 0:
      temp_list.append(item)

   elif len(temp_list) > 0:
      if  temp_list[-1] != item:
          temp_list.append(item)

print(temp_list)
  1. Fetch each item from the main list(list1).
  2. If the 'temp_list' is empty add that item.
  3. If not , check whether the last item in the temp_list is not same as the item we fetched from 'list1'.
  4. if items are different append into temp_list.
like image 27
Fuji Komalan Avatar answered Oct 19 '22 02:10

Fuji Komalan


If you want to use the itertools method @MaxU suggested, a possible code implementation is:

import itertools as it

lst=[1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]

unique_lst = [i[0] for i in it.groupby(lst)]

print(unique_lst)
like image 41
Ben Avatar answered Oct 19 '22 01:10

Ben