Reputation: 1923
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]
Upvotes: 3
Views: 59
Reputation: 2218
You should try this :
newList = [1,2,3,4,5,2,6,7,5,8]
for num in newList:
if newList.count(num) > 1:
for i in newList.count(num):
newList.remove(num)
print(newList)
Upvotes: 0
Reputation: 1967
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
Upvotes: 1
Reputation: 139
Try This:
newList = [1,2,3,4,5,2,6,7,5,8,9,9,9,9]
l = []
for i,v in enumerate(newList):
if v in l:
l.pop(l.index(v))
else:
l.append(v)
print(l)
Upvotes: 0