mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Remove duplicates while preserving order
This commit is contained in:
@@ -180,3 +180,17 @@ def get_music_dir():
|
|||||||
# respectively is sufficient.
|
# respectively is sufficient.
|
||||||
# On Linux, default to /home/<user>/Music if the above method failed.
|
# On Linux, default to /home/<user>/Music if the above method failed.
|
||||||
return os.path.join(home, 'Music')
|
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))]
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ def download_list(text_file):
|
|||||||
with open(text_file, 'r') as listed:
|
with open(text_file, 'r') as listed:
|
||||||
# read tracks into a list and remove any duplicates
|
# read tracks into a list and remove any duplicates
|
||||||
lines = listed.read().splitlines()
|
lines = listed.read().splitlines()
|
||||||
lines = list(set(lines))
|
lines = internals.remove_duplicates(lines)
|
||||||
# ignore blank lines in text_file (if any)
|
# ignore blank lines in text_file (if any)
|
||||||
try:
|
try:
|
||||||
lines.remove('')
|
lines.remove('')
|
||||||
|
|||||||
Reference in New Issue
Block a user