Remove duplicates while preserving order

This commit is contained in:
Ritiek Malhotra
2018-09-26 10:45:48 +05:30
parent 32c2ace96c
commit 95139222d0
2 changed files with 15 additions and 1 deletions

View File

@@ -180,3 +180,17 @@ def get_music_dir():
# respectively is sufficient.
# On Linux, default to /home/<user>/Music if the above method failed.
return os.path.join(home, 'Music')
def remove_duplicates(tracks):
"""
Removes duplicates from a list whilst preserving order.
We could directly call `set()` on the list but it changes
the order of elements.
"""
local_set = set()
local_set_add = local_set.add
return [x for x in tracks
if not (x in local_set or local_set_add(x))]