Saturday, November 9, 2019

python - One-liner to remove duplicates, keep ordering of list





I have the following list:



['Herb', 'Alec', 'Herb', 'Don']



I want to remove duplicates while keeping the order, so it would be :



['Herb', 'Alec', 'Don']


Here is how I would do this verbosely:



l_new = []
for item in l_old:
if item not in l_new: l_new.append(item)



Is there a way to do this in a single line?


Answer



You could use an OrderedDict, but I suggest sticking with your for-loop.



>>> from collections import OrderedDict
>>> data = ['Herb', 'Alec', 'Herb', 'Don']
>>> list(OrderedDict.fromkeys(data))
['Herb', 'Alec', 'Don']



Just to reiterate: I seriously suggest sticking with your for-loop approach, and use a set to keep track of already seen items:



>>> data = ['Herb', 'Alec', 'Herb', 'Don']
>>> seen = set()
>>> unique_data = []
>>> for x in data:
... if x not in seen:
... unique_data.append(x)

... seen.add(x)
...
>>> unique_data
['Herb', 'Alec', 'Don']


And in case you just want to be wacky (seriously don't do this):



>>> [t[0] for t in sorted(dict(zip(reversed(data), range(len(data), -1, -1))).items(), key=lambda t:t[1])]
['Herb', 'Alec', 'Don']


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...