Replace duplicate items from list while keeping the first occurrence

  • A+
Category:Languages

I have a list lst = [1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,4,4]

I'm expecting the following output:

out = [1,"","",2,"","","",3,"","","","",4,"","","","","","","",""] 

I want to keep the first occurrence of the item and replace all other occurrences of the same item with empty strings.

I tried the following approach.

`def splrep(lst):     from collections import Counter     C = Counter(lst)     flst = [ [k,]*v for k,v in C.items()]     nl = []     for i in flst:         nl1 = []         for j,k in enumerate(i):             nl1.append(j)         nl.append(nl1)      ng = list(zip(flst, nl))     for i,j in ng:         j.pop(0)     for i,j in ng:         for k in j:             i[k] = ''     final = [i for [i,j] in ng]     fin = [i for j in final for i in j]     return fin` 

But I'm looking for some simpler or better approaches.

 


Use itertools.groupby, quite appropriate for grouping consecutively duplicate values.

from itertools import groupby [v for k, g in groupby(lst) for v in [k] + [""] * (len(list(g))-1)] # [1, '', '', 2, '', '', '', 3, '', '', '', '', 4, '', '', '', '', '', '', '', ''] 

If your list values are not consecutive, you may sort them first.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: