Skip to content

Instantly share code, notes, and snippets.

@anubhavsinha
Created September 1, 2012 16:27
Show Gist options
  • Save anubhavsinha/3579441 to your computer and use it in GitHub Desktop.
Save anubhavsinha/3579441 to your computer and use it in GitHub Desktop.
list vs set: Python
#function to remove duplicates from a Python list
#using append/add is O(1) for both list and set
#but using in/not in/remove in list is O(n) vs O(1) in case of set
def remove_dupes(some_list):
seen = set()
items = []
for each in some_list:
if each not in seen:
seen.add(each)
items.append(each)
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment