0

How can I return only the element/s with no duplicate in the list?

e.g.

list = [1,4,5,1,5] to [4]
mmvsbg
  • 3,570
  • 17
  • 52
  • 73
  • 1
    You can find some very good clues on how to do this [here](https://stackoverflow.com/a/9836685/102937). – Robert Harvey Nov 22 '18 at 03:07
  • 1
    Or from [another answer to the same question](https://stackoverflow.com/a/49865122/1270789), then `list(set(uniques)-set(dups))`. – Ken Y-N Nov 22 '18 at 03:14
  • write a function `def uniques(x): a,b=np.unique(x,return_counts=1);return a[b==1]` then use it: `uniques( [1,4,5,1,5])` – Onyambu Nov 22 '18 at 03:59
  • StackOverflow is not a code-writing service. Please read through the [Help Center](https://stackoverflow.com/help), in particular [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include [Minimum, Complete, Verifiable Example](https://stackoverflow.com/help/mcve). People will be glad to help – Andreas Nov 22 '18 at 04:25

2 Answers2

0

You can use Counter()

from collections import Counter as counter 
list = [1,4,5,1,5]
c = counter(list)

print(*[item for item, time in c.items() if time==1], sep='\n')

Output :

C:\Users\Desktop>py x.py
4
Rarblack
  • 4,559
  • 4
  • 22
  • 33
0
def unique(list1):
for i in list1:
    if(list1.count(i)==1):
        return i 
list1 = [10, 20, 10, 30, 40, 40]  
print(unique(list1))


list2 =[1, 2, 1, 1, 3, 4, 3, 3, 5]  
print(unique(list2))
mohor chatt
  • 356
  • 1
  • 8