-4

How to remove duplicate elements from the list.

I have

list=[1,2,1,2,3,1,3,4]

and I want to drop repeated items. That is, if an item in the list comes two or more times then it is exclude from the list completely. The final list should look like this:

new_list=[4]

In this example 1, 2 and 3 occur at least two time and therefore all these elements are removed from the list

list=[1,2,1,2,3,1,3,4]
new_list = [i for i in set(list)] # wrong; how do I fix this?
print new_list

Please note this is different from set(list) because set keeps all items in the list.

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
user_123
  • 426
  • 7
  • 23
  • with 2 lines of code, I can't see any here... – Julien Nov 09 '17 at 07:05
  • 2
    and duplicate suggestion is wrong... – Julien Nov 09 '17 at 07:08
  • Please take a look at the [How to Ask](https://stackoverflow.com/help/how-to-ask) page. StackOverflow users expect that a question show some effort - it is not a code writing service. – SethMMorton Nov 09 '17 at 07:08
  • I already used sets, remove, unique but in all output look like new_list=[1,2,3,4] but it want only new_list=[4] and remove 1,2 and 3 from list. – user_123 Nov 09 '17 at 07:10
  • @SethMMorton I agree with Julien that duplicate classification is incorrect. Please re-open this question. – AGN Gazer Nov 09 '17 at 07:20

1 Answers1

1
a = [1,2,1,2,3,1,3,4]

from collections import Counter
[i[0] for i in Counter(a).items() if i[1] == 1]

An even simpler answer is:

[i for i in set(a) if a.count(i) == 1]
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45