From 95139222d01db7f810cefe5af8fbc793ce40e9f5 Mon Sep 17 00:00:00 2001 From: Ritiek Malhotra Date: Wed, 26 Sep 2018 10:45:48 +0530 Subject: [PATCH] Remove duplicates while preserving order --- spotdl/internals.py | 14 ++++++++++++++ spotdl/spotdl.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spotdl/internals.py b/spotdl/internals.py index 2461901..ca8b6e4 100755 --- a/spotdl/internals.py +++ b/spotdl/internals.py @@ -180,3 +180,17 @@ def get_music_dir(): # respectively is sufficient. # On Linux, default to /home//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))] diff --git a/spotdl/spotdl.py b/spotdl/spotdl.py index 12a5e98..c928a80 100755 --- a/spotdl/spotdl.py +++ b/spotdl/spotdl.py @@ -68,7 +68,7 @@ def download_list(text_file): with open(text_file, 'r') as listed: # read tracks into a list and remove any duplicates lines = listed.read().splitlines() - lines = list(set(lines)) + lines = internals.remove_duplicates(lines) # ignore blank lines in text_file (if any) try: lines.remove('')