0

I am trying to create a function that returns a unique value on a python list. So for example:

List = [1,1,1,1,2,1,1,1,1,]

How would I go about returning the unique value, 2.

I have tried the following:

import numpy as np

def find_uniq(arr):
    abc = np.array(arr)
    return list(np.unique(abc))

However when I try this ^^^ , it returns (2) two values , the 1 and the 2. Since they are both unique values to the list. Though I only want the 2.

3
  • Are you trying to find the element which appears only once (assuming this is always the case)? You can try count method for each unique value and return the element with count equal to 1. Commented Jul 10, 2021 at 23:34
  • You could use a Counter object from the collections module Commented Jul 10, 2021 at 23:34
  • @Carlos, that is correct. This is what I will most likely be doing. Commented Jul 11, 2021 at 0:48

7 Answers 7

1

Try this:

>>> from collections import Counter
>>> c = Counter( [1,1,1,1,2,1,1,1,1,])
>>> c
Counter({1: 8, 2: 1})
>>> print ([k for k,v in c.items() if v==1])
[2]

This will tell you quickly if there is no unique element, or more than one.

Sign up to request clarification or add additional context in comments.

Comments

0

Return counts from numpy.unique and then select values that have count of 1:

lst = [1,1,1,1,2,1,1,1,1]
val, cnt = np.unique(lst, return_counts=True)
val[cnt == 1]
# [2]

Comments

0

You can loop through the unique elements using set, and return the element which appears only once:

for ele in set(arr):
    if arr.count(ele) == 1:
        print(ele)
        break

Comments

0

For every unique element append it to list

def find_unique(nums_list):
    result = []
    for i in nums_list :
        if nums_list.count(i) == 1 :
            result.append(i)
    return result

print(find_unique([1,1,1,1,2,1,1,1,1]))

If you want to find only first unique element

def find_unique(nums_list):
    for i in nums_list :
        if nums_list.count(i) == 1 :
            return i
    
print(find_unique([1,9,1,1,2,1,1,1,1]))

Comments

0
def check_window(i,a):
    return all(a[i+d]!=a[i] for d in (-1,1))

l = [1,1,1,1,2,1,1,1,1]
ls = sorted(l)
ls.append(None)

uniques = [ls[i] for i in range(1,len(ls)-1) if check_window(i, ls)]
    

Comments

0
for i in numList:
    if numList.count(i) == 1 :
        print ("unique", i) 

Comments

0

Encountered on Codewars ('Find the unique number' by isqua)

The first solution I came up with below, has an issue where it takes too long to return what I need. It doesn't create additional arrays, but I guess iterating through a giant list would be a problem.

for n in arr:
        if arr.count(n) == 1:
            return n
        else: None

The method below worked perfectly, and leads me to believe that python sorts faster than iterating.

x,y = sorted(arr)[0], sorted(arr)[-1]
return x if arr.count(x) == 1 else y

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.