[Python] for.. not in.. Remove Deduplication

Write a function, remove_duplicates that takes a list as its argument and returns a new list containing the unique elements of the original list. The elements in the new list without duplicates can be in any order.

Suggested test cases: Try an input list with no duplicate elements. The output list should be the same size as the original list. Try a small input list with a known number of unique entries, and some duplicates. Verify that the list without duplicates has the correct length.

def remove_duplicates(source):
    target = []

    for element in source:
        if element not in target:
            target.append(element)

    return target

 

posted @ 2017-11-21 14:52  Zhentiw  阅读(341)  评论(0编辑  收藏  举报