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.
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.
drop_duplicates() will keep the first instance of a duplicate row and remove any others.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With