Sunday, August 19, 2018

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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...