-1

I am trying to remove duplicate elements from a list. The code is removing the wrong elements.

def simplify(A):
    for i in A:
        for j in A:
            if i == j:
                A.remove(j)
    return A

Input A:

A = [1, 2, 5, 8, 12, 5]

Output:

A = [2, 8]
Austin
  • 83
  • 8
  • `set([1,1,2])` will return `{1, 2}`, note that in python `[1, 1, 2]` is not a set, it's a list. – cglacet Mar 12 '19 at 02:27
  • Don't change a list while iterating over it, you'll get some unexpected results – rafaelc Mar 12 '19 at 02:28
  • ...Your loop, if it worked, would remove every element, since every element equals itself. What ought to be surprising to you is that any elements are left at all. Also read PEP8 for naming conventions; you never start method arguments with an upper case letter. – jpmc26 Mar 12 '19 at 02:39

1 Answers1

1

Try this.

def RemoveDuplicates(list_):
    return list(set(list_))

You can call this function by passing your list and you get back listwithout duplicates.

Hayat
  • 1,539
  • 4
  • 18
  • 32