Remove Duplicates


#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/removeduplicates?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>
remove_duplicates([4, 5, 5, 4]) resulted in an error: list index out of range

<What do you expect to happen instead?>
Index should not go out of range.


def remove_duplicates(numbers):
  new_list = []
  for index in numbers:
      new_list.append(numbers[0])
      if (numbers[index] == numbers[index-1] and index != 0):
        new_list.append(numbers[index])
  return new_list

Can’t able to figure out how to keep the index within the range? Because to compare two consecutive elements I need to use index-1 or index+1. Both of the times index is going out of range.
Also, should I remove the duplicate elements along with copying the elements into a new list?


#2

your variable index doesn’t contain indexes, it contains values from the list, prove:

def remove_duplicates(numbers):
  new_list = []
  for index in numbers:
      print index
      new_list.append(numbers[0])
      if (numbers[index] == numbers[index-1] and index != 0):
        new_list.append(numbers[index])
  return new_list

print remove_duplicates([4, 5, 5, 4])

i added a print statement, to see the values of index variable, and a function call so the function actually executes.

Given values from the list might be higher then the indexes, there is a good change you get an index error.


#3

Hello, after your advice I modified the code as below:

def remove_duplicates(numbers):
  new_list = []
  new_list.append(numbers[0])
  j = 0
  for index in range(len(numbers)):
    if index == 0:
      continue
    elif index != 0:
      while j < index:
        if (numbers[index] != numbers[j]):
          new_list.append(numbers[index])
          j += 1
  return new_list

print remove_duplicates([4, 5, 5, 4])

But, my code is neither getting executed nor throwing any errors nor printing anything. Can you please guide?


#4

you could have an infinity loop, then the code execute but the execution goes on until infinity

This is not going anywhere, what you should do is the following:

loop over numbers
inside this loop, nest another for loop to loop over new_list.

so for example:

for number in numbers:
    for item in new_list:

check if number and item are equal, if so, break the second for loop.

then we use a for/else on the second loop, if the loop gets successfully through all the items in new_list without breaking, append number to new_list in else clause (else won’t execute in for/else if the loop gets broken by break keyword)

you could save yourself on the second for loop by using the in keyword


#5
def remove_duplicates(numbers):
  new_list = []
  flag = 1
  for number in numbers:
    for item in new_list:
      if number == item:
        flag = 0
        break
    if(flag != 0):
      new_list.append(number)
  return new_list

I tried a new code. Took the flag to rule out the possibility that if the element is not matching with the initial elements but can match with other elements, hence it will not be included again. Please share your views why every input is not working here?

Error: remove_duplicates([88, 5, 7, 88, 93, 4]) returned [88, 5, 7] instead of [88, 4, 5, 93, 7]. I doubt if the elements get matched, it is not going back to the original loop.


#6

why complicate matters with a flag variable?

Why not simply do:

for item in new_list:
   if number == item:
     break
else:
   # appending

else only runs when loop finishes running, not when broken by break keyword


#7

Thanks that helped. I didn’t know about for/else…learnt a new thing. :slight_smile:


#8

if you really want to use flag, you should do:

for number in numbers:
    flag = 1

this will ensure flag is set for each number in our numbers list, then if there is a match, flag changes, nothing gets added, if no matches, flags stays unchanged, so number is appended

but for/else is better


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.