Skip to content

Instantly share code, notes, and snippets.

@Hwatwasthat
Created February 22, 2018 11:10
Show Gist options
  • Save Hwatwasthat/bc104fbd44477f3373287a97d835a703 to your computer and use it in GitHub Desktop.
Save Hwatwasthat/bc104fbd44477f3373287a97d835a703 to your computer and use it in GitHub Desktop.
List no duplicates created by Hwatwasthat - https://repl.it/@Hwatwasthat/List-no-duplicates
import random
list = []
i = 0
while i < 100:
list.append(random.randrange(1,40))
i += 1
# creating a list with no duplicates using a for loop
def list_no_duplicates(list):
# sorting list to make it pretty
list.sort()
new_list = []
# iterating over the list for the whole length
for i in range(len(list)):
#checking to ensure the value is not already in the list
if list[i] not in new_list:
#adding a unique value to the end of the list
new_list.append(list[i])
#returning the list
return new_list
print(list_no_duplicates(list))
#sorting the list using a set, which never has duplicates
new = (set(list))
print(new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment