H
H
Hcuy2021-12-09 16:48:18
Python
Hcuy, 2021-12-09 16:48:18

How to correctly remove duplicate elements in lists?

Hello everyone, I have already asked a similar question, but I did not quite correctly describe it. In short:

a = [1,2,1,3]
b = [1,2]

The output should look like this . Tried like this:
[1,3]
test_2 = [1,2,3,4]
my_paper = [3,1,2 ]
test2 = [int(item) for item in test_2]
for i in test_2:
    for j in my_paper:
        if i == j:
            test2.remove(i)
            my_paper.remove(j)
print (test2)
print (my_paper)

But they explained that it is wrong to do this, how to do it right? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-12-09
@Hcuy

from collections import Counter

count = Counter(a)
count.subtract(Counter(b))

result = sum(([ k ] * v for k, v in count.items()), [])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question