Last active
August 26, 2016 14:06
-
-
Save tblong/5599b957e2738def10fb5d403a57e713 to your computer and use it in GitHub Desktop.
Remove duplicates from list of numbers
This file contains hidden or 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
#!/usr/bin/env python | |
# | |
# Remove duplicates from list of numbers | |
# | |
# @author tblong | |
# | |
# 2016-08-26: Added dictionary lookup insead of walking | |
# the entire list each time. The list will still maintain | |
# its original order. | |
# | |
list_0f_nums = [1, 2, 2, 3, 5, 6, 8, 8] | |
def remove_duplicates(numbers): | |
result = [] | |
lookup = {} | |
for num in numbers: | |
if lookup.get(num) is None: | |
lookup[num] = num | |
result.append(num) | |
return result | |
def main(): | |
print "List with duplicates removed:", str(remove_duplicates(list_0f_nums)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment