Ask Question
21 August, 19:41

Write a function named remove_duplicates that takes a list (of numeric and/or string values) and returns a new list with only the unique elements from the original. This function assumes that there is at least 1 element in the list and that the list does not contain any sublists. The original list is not modified in any way. The returned list does not have to preserve the order of the original list.

+4
Answers (1)
  1. 21 August, 20:24
    0
    def remove_duplicates (lst):

    no_duplicate = []

    dup = []

    for x in lst:

    if x not in no_duplicate:

    no_duplicate. append (x)

    else:

    dup. append (x)

    for y in dup:

    if y in no_duplicate:

    no_duplicate. remove (y)

    return no_duplicate

    Explanation:

    Create a function called remove_duplicates that takes one parameter, lst

    Create two lists one for no duplicate elements and one for duplicate elements

    Create for loop that iterates through the lst. If an element is reached for first time, put it to the no_duplicate. If it is reached more than once, put it to the dup.

    When the first loop is done, create another for loop that iterates through the dup. If one element in no_duplicate is in the dup, that means it is a duplicate, remove that element from the no_duplicate.

    When the second loop is done, return the no_duplicate
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function named remove_duplicates that takes a list (of numeric and/or string values) and returns a new list with only the unique ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers