Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Created December 5, 2019 01:15
Show Gist options
  • Save FerdinaKusumah/b85cf132a6a3f041bd82121579824d8f to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/b85cf132a6a3f041bd82121579824d8f to your computer and use it in GitHub Desktop.
Remove Duplicate List Or Dict
from collections import OrderedDict
"""Remove duplicate in list"""
a = [1, 1, 3, 4, 6, 7, 4, 6, 2, 1]
remove_duplicate_list = list(set(a))
print(remove_duplicate_list)
# [1, 2, 3, 4, 6, 7]
"""Remove duplicate in dict and keep order"""
b = ["apple", "banana", "grape", "banana", "watermelon"]
remove_dup_keep_order = OrderedDict.fromkeys(b).keys()
print(remove_dup_keep_order)
# odict_keys(['apple', 'banana', 'grape', 'watermelon'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment