0

I am learning python from Youtube and want to write a python code which removes duplicates from the list. This is my code :

nums = [1,1,2,2,2,2,3,3,4,8,5,6,6,4,4,7,5,8]
j = 1
for i in nums :
    if nums.count(i) > 1 :
        while j < nums.count(i) :
            nums.remove(i)
            j += 1
print(nums)

I know there are other ways to do it but i want to know why this one isn't working. are the all the modules correct?

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

You are not resetting your counter (j) each time and it continues to increment. Try moving the j = 1 into the for loop.

But you shouldn't ever mutate a list as you iterate through it.

myz540
  • 537
  • 4
  • 7