0

Suppose i have the following list:

my_list = ["sam","sam", "is", "a", "is", "sam" , "good", "person", "person"]

Now my requirement is to write a function that selectively removes duplicates. I want to use this function in a for loop.

suppose i want to remove "sam" duplicates

Which means after 1st iteration my desired result is as follows:

my_list  = ["sam", "is", "a", "is", "good", "person", "person"]

only duplicates of "sam" are removed.

similarly, after the second iteration, i want to remove is "person", so my list will look like:

 my_list  = ["sam", "is", "a", "is", "good", "person"]

Please suggest a way i can do this?

Thanks & Regards

Samit Saxena
  • 99
  • 1
  • 9
  • so in each iteration, it should ask for the removing item name? – Vikas Periyadath Jun 25 '20 at 06:25
  • something like following: for i in input_list:---> so i will be that string that we need to remove. ---> basically it is selected automatically. So when i == "sam" my aim is to call a function which will remove all the occurances of "sam" which are in the indices ahead of this selected one – Samit Saxena Jun 25 '20 at 06:28
  • 1
    Not clear, add in the question itself please – Vikas Periyadath Jun 25 '20 at 06:33

3 Answers3

1

The cleanest way do do it I think would be to use list comprehension to remove all the appeareances of the element, and then add the element to the end. You can do something like that:

def removeDuplicates(my_list,current):
    return [ element for element in my_list if element != current] + [current]

And calling the function:

>>> my_list
['sam', 'sam', 'is', 'a', 'is', 'sam', 'good', 'person', 'person']

>>> my_list2 = removeDuplicates(my_list,"sam")
>>> my_list2
['is', 'a', 'is', 'good', 'person', 'person', 'sam']

>>> my_list3 = removeDuplicates(my_list2,"person")
>>> my_list3
['is', 'a', 'is', 'good', 'sam', 'person']
CanciuCostin
  • 1,773
  • 1
  • 10
  • 25
0

try this,

def remove(key, value):
    filter_ = []
    for v in value:
        if key == v and key in filter_:
            continue

        filter_.append(v)
    return filter_


my_list = ["sam","sam", "is", "a", "is", "sam" , "good", "person", "person"]

my_list = remove("sam", my_list)
print(my_list)

my_list = remove("person", my_list)
print(my_list)

['sam', 'is', 'a', 'is', 'good', 'person', 'person']

['sam', 'is', 'a', 'is', 'good', 'person']
sushanth
  • 8,275
  • 3
  • 17
  • 28
-1

For selective thing you need to iterate over the list:

def remove_duplicate_selection(my_list, item):
    flag = False
    length = len(my_list)
    for i in range(length):
        if length == i:
            break
        if my_list[i] == item:
            if flag:
                del my_list[i]
                length -= 1
            else:
                flag = True
    return my_list


def main():
    my_list = ["sam", "sam", "is", "a", "is", "sam", "good", "person", "person"]
    remove_duplicate_selection(my_list, "sam")
    print(my_list)
    remove_duplicate_selection(my_list, "person")
    print(my_list)


if __name__ == '__main__':
    main()

output should be:

['sam', 'is', 'a', 'is', 'good', 'person', 'person']
['sam', 'is', 'a', 'is', 'good', 'person']