In a list of three elements I want to find the element of a list that is different from the other two.
For example I have L=[3,3,1]
and I want to extract the number 1. At first I was thinking about using set()
so I can compare it with a list but then I got stuck again in this code:
S=set(L)
c1=0
c2=0
for k in L:
for i in S:
if k==i:
c1+=1
v1=k
else:
c2+=1
v2=k
if c1<c2:
print (v1)
else:
print (v2)
I tried to count how many times they appear so I can be sure with c1 and c2 will have a different value but then I realized v1 and v2 were completely wrong.
Find the rarest?
>>> min(L, key=L.count)
1
Or if they're all ints, make the duplicate pair cancel each other with xor:
>>> L[0] ^ L[1] ^ L[2]
1