- Code: Select all
`def remove_duplicates(numbers):`

new_list = [] # To initialize new_list

numbers.sort()

#

# To test if the list is empty

#

if len(numbers) == 0:

new_list = []

return new_list

#

# To test if there is only one integer in the list.

#

if len(numbers) == 1:

new_list.append(numbers[0])

return new_list

#

# To test if one integer is equal to the next integer

# The range has to be one short or you will go over the index

#

new_list.append(numbers[0])

j = 0

k = 1

while k < len(numbers)-1:

if numbers[j] != numbers[k]:

new_list.append(numbers[k])

j = j + 1

k = k + 1

if numbers[j] == numbers[k]:

j = j + 1

k = k + 1

return new_list

numbers = input("Enter a list of integers: ")

print(remove_duplicates(numbers))

It works for a few things. The problem I am having is that I keep getting index out of bounds if I change any the main section, for example while j < len(numbers)-1 where I substitute k with j or add an equal sign instead of the less than sign. If I have five elements in the list it will not add the fifth element for example [1,2,3,4,5] will give [1,2,3,4]. Like I said before, if I change it to get one more loop it gives me index out of bounds. I am at a loss. Surely, it would work with the right combination? It is the only problem I am having before I can move on to classes. Thanks, Micah.