Python : removing other appearances of an item in a list

Question:

I was triying to make a program that can remove any other appearnces of an item in a list but it seems that there is something that I don’t understand about loops

list = [3, 2, 1, 4, 2, 3, 2]

list2 = list.copy()
list2.reverse()

l = len(list)
for i in range(l - 1):
    for j in range(i+1, l):
        if list[i] == list[j]:
            h = list[j]
            list2.remove(h)
            



list2.reverse()
print(list2)

the output: [3, 1, 4]
expected : [3, 2, 1, 4] (I got it after adding "break" under list2.remove(h))

my question is why is "break" necessary here ? the block under if should have been executed only once even without using "break".
in other way when i =1 and j = 4 the if conditon is met so the block is executed removing "2" once from list2
then when i=1 and j=6 "2" is removed again. so why did another "2" get removed ?

Asked By: Moha Med

||

Answers:

The reason why you need to add a break statement after list2.remove(h) is because without it, your inner loop will continue to execute even after you’ve removed an element from list2. This means that if there are multiple occurrences of the same item in list, you will end up removing all of them from list2 even though you only want to remove all but one.

When i = 1 and j = 4, the if condition is met because list[i] and list[j] both equal 2. This means that h = 2 and list2.remove(h) is executed, removing the last occurrence of 2 from list2. However, the inner loop continues to execute, and when j = 6, the if condition is met again because list[i] and list[j] both equal 2. This means that h = 2 again and list2.remove(h) is executed, removing the second-to-last occurrence of 2 from list2.

By adding a break statement after list2.remove(h), you exit the inner loop as soon as you remove an element from list2, which ensures that you only remove one occurrence of each item from list2.

Answered By: charlie10

You can just create a new list instead of copying existing one and check if item from list A is in list B. If element a is in list B then just pass it and go on next element

lista = [3, 2, 1, 4, 2, 3, 2]
listb = []
for a in lista:
    if a not in listb:
        listb.append(a)
print(listb)

[3, 2, 1, 4]
Answered By: Anivaries

The easiest way to do this is to use a set. A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence.

to use it on your list you can do:

initial_list = [3, 2, 1, 4, 2, 3, 2]
unique_list = list(set(initial_list))

Now, for the reason why your code didn’t work is that the first 2 is matched twice in the loop and then second one is matched once so you are deleting 2 3 tims.

Answered By: zaki98

While this doesn’t answer your question about break, one of the easier ways to remove duplicates from a list is to use sets.

A set is a collection type that doesn’t allow duplicate values. Converting your list into a set and back into a list will remove the duplicates:

my_list = [3, 2, 1, 4, 2, 3, 2]
my_set = set(my_list) #{1, 2, 3, 4}
my_filtered_list = list(my_set) #[1, 2, 3, 4]
Answered By: Gabe
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.