0

I was writing a function to remove duplicate integers in a list, but I don't think I did it the most efficient way possible. Any suggestions?

def remove_dups(items):
    new_list = sorted(items)
    i_postion = 0
    if len(new_list) > 1:
        for i in new_list:
            counter = 1
            while counter < len(new_list):
                if i_postion + counter < len(new_list):
                    if new_list[i_postion] == new_list[i_postion + counter]:
                        new_list.remove(new_list[i_postion + counter])
                counter += 1
            i_postion += 1

        #A check if the list is only one number multiple times
        if new_list[0] == new_list[1]:
            new_list.remove(new_list[1])
    else:
        return(new_list)

    return(new_list)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3451049
  • 23
  • 1
  • 4

1 Answers1

1

If you don't need the order and all elements of list L are immutable, use a set:

L = [3, 4, 5, 4, 2, 3, 5]
>>> res = list(set(L))     

If you need to preserve order:

res = []
seen = set()
for ele in L:
    if ele in seen:
        continue
    res.append(ele)
    seen.add(ele)

>>> res
[3, 4, 5, 2]
Mike Müller
  • 82,630
  • 20
  • 166
  • 161