0

I've a list that contains thousands of Instagram usernames and some of them are repeated 2 or 3 times. I want to remove all the duplicates in one function but when I try to remove all the duplicates some of them remains in the list and are removed when I call that function again and again.

>>> def rem():
   for n in m:
       if m.count(n) > 1:
           m.remove(n)
   print(len(m))


>>> rem()
231
>>> rem()
200
>>> rem()
200

Here is the sample List:

>>> a = list(range(100))
>>> b = list(range(150))
>>> c = list(range(40, 100))
>>> d = list(range(100, 200))
>>> m = a+b+c+d

1 Answers1

0

Just convert it into a set, then back to a list:

def rem():
    m = list(set(m))
    print(len(m))

Or even better, only ever store them in a set. Sets must have unique keys, meaning it won't ever store duplicates.

Jacob
  • 1,697
  • 8
  • 17