Created
September 1, 2012 16:27
-
-
Save anubhavsinha/3579441 to your computer and use it in GitHub Desktop.
list vs set: Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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