Skip to content

Instantly share code, notes, and snippets.

@tblong
Last active August 26, 2016 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tblong/5599b957e2738def10fb5d403a57e713 to your computer and use it in GitHub Desktop.
Save tblong/5599b957e2738def10fb5d403a57e713 to your computer and use it in GitHub Desktop.
Remove duplicates from list of numbers
#!/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