14 remove duplicates why doesn't it work :(


#1

The code works, but it keeps telling me "oops try again"

def count(sequence, item):
    result = 0
    for i in sequence:
        if i == item:
            result += 1
    return result

def remove_duplicates(lst):
   
    for t in lst:
        while count(lst, t) > 1:
            lst.remove(t)
    return lst

#2

If you want simpler code you could do this exercise in just one function.

  1. First of all get rid of the first function 'count'.
  2. Then the argument for the remove_duplicates function change it to l.
  3. Then make an empty list called lst. (Don't do "list" because that is a default function in python).
  4. Next you should make the for loop "for i in l:" this goes through the argument "l".
  5. After that with an if statement inside of the for loop check if i is not inside "lst" (the empty list you made).
  6. Inside of the if statement write in the code 'lst.append(i)" this puts i inside lst if the value meets the conditions of the if statement.
  7. finally you should return lst

Your final code should look like this:

def remove_duplicates(l):
lst = []
for i in l:
if i not in lst:
lst.append(i)
return lst


#3

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