+ 1

How to remove paired items in a list?

How to remove paired items in a list? Let's say I have a list List = [1,3,1,3,2, 1] You need to remove 2 paired elements from the list That is, first remove 1, 1. Then 3, 3. The output should be [1,2] The number 1 remains because she does not have a pair

13th Nov 2019, 12:58 PM
Марсель Халимов
5 Antworten
+ 3
There is a contradiction in your statement according which elements should be in the output. Can you check this please?
13th Nov 2019, 1:56 PM
Lothar
Lothar - avatar
+ 1
Sorry. I changed my question
14th Nov 2019, 4:33 AM
Марсель Халимов
0
for i in list: if list.count(i)%2==0: for j in range(list.count(i)): list.remove(i) for i in list: if list.count(i)%2>=1: for j in range(list.count(i)-1): list.remove(i) you convert it to a function. print('good luck')
14th Nov 2019, 6:20 PM
Mohammad Gk
Mohammad Gk - avatar
- 1
If you just want to remove duplications: print(sorted(list(set(List)))) If you want to remove them completely: print([i for i in List if List.count(i) == 1])
13th Nov 2019, 1:41 PM
Qasem