However the way i followed below is just removing one time
newList = [1,2,3,4,5,2,6,7,5,8]
for num in newList:
if newList.count(num) > 1:
newList.remove(num)
print(newList)
[1, 3, 4, 2, 6, 7, 5, 8]
There's 2 issues with your code:
help(list.remove) explicitly says it'll "remove first occurrence of value" With a couple of print in your code, this becomes obvious:
newList = [1,2,3,4,5,2,6,7,5,8]
for num in newList:
print (num)
if newList.count(num) > 1:
print(' -->removing')
newList.remove(num)
print(newList)
outputs:
1
2
-->removing
4
5
-->removing
6
7
5
8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With