14. remove_duplicates


#1


https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096

My code is either skipping the first if statement or the second for loop. Either way, it won't append anything into edit so the program continues to return [].

"Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [] instead of [4, 5]"


def remove_duplicates(lst):
    edit = []
    for n in lst:
        if lst.count(n) > 1:
            for i in edit:
                if n != i:
                    edit.append(n)
        else:
            edit.append(n)
    return edit

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


#2

Since we are creating a new list, how about using it as a lookup? We would iterate over the argument list, and if the item is not found in the lookup list, append it, otherwise continue.


#4

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