G
G
Gadel Imamov2018-10-20 15:11:55
Python
Gadel Imamov, 2018-10-20 15:11:55

Is there a function in Python that finds a different unit of a list?

Given a list, let a = [1, 1, 1, 1, 2, 1, 1, 1]. In this list, all elements are the same, except for one (in this case, 2). The question is, is there such a function in python that finds this very element that is different from the rest?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2018-10-20
@Gadelius

from collections import Counter

rarest = Counter(a).most_common()[-1][0]

D
Dimonchik, 2018-10-20
@dimonchik2013

rarest =  [y for y in list(set(a)) if len([x for x in a if x == y]) == 1][0]

a.sort(); 
rarest = set(a[:2]) - set(a[-2:])

M
mentor2, 2018-10-20
@mentor2

>>> while min(a) != max(a):
... a1 = a.pop()
...
>>> a1
2
>>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question