number = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1]
number.reverse()
for element in number:
if number.count(element) > 1:
number.remove(element)
number.reverse()
# when I execute this program for this particular list the duplicates are not removed why?
Asked
Active
Viewed 55 times
-2

John Gordon
- 29,573
- 7
- 33
- 58
-
2Don't remove items from a list while you're iterating over the list. Confusing things happen, just like this. – John Gordon Jul 08 '22 at 02:35
-
removing items from a list while iterating over it would make iterations shorter and may not go through entire list. – vman Jul 08 '22 at 02:37
1 Answers
2
use set
number = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1]
print(set(number))
output is {1, 2, 3, 4, 5}

John Beats
- 59
- 1
- 6