Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace duplicate items from list while keeping the first occurrence

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.

like image 683
sharathchandramandadi Avatar asked Jan 04 '19 10:01

sharathchandramandadi


People also ask

How do you remove duplicates from a list while keeping the same order of the elements?

If you want to preserve the order while you remove duplicate elements from List in Python, you can use the OrderedDict class from the collections module. More specifically, we can use OrderedDict. fromkeys(list) to obtain a dictionary having duplicate elements removed, while still maintaining order.

How do I remove duplicates instance and keep First Instance in Python?

drop_duplicates() will keep the first instance of a duplicate row and remove any others.

How do you remove duplicates from a list in Python with for loop?

You can make use of a for-loop that we will traverse the list of items to remove duplicates. The method unique() from Numpy module can help us remove duplicate from the list given. The Pandas module has a unique() method that will give us the unique elements from the list given.


1 Answers

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.

like image 86
cs95 Avatar answered Oct 10 '22 21:10

cs95